node.js中实现GET&POST请求

创建基本的服务器

const express = require('express');
const indexRouter = require('./router'); // 引入路由
const app = express();
const port = 3000;
// 挂载路由
app.use('/api', indexRouter);
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

创建路由文件

const express = require('express');
const router = express.Router();

module.exports = router;

实现GET请求

// 处理GET请求
router.get('/get', (req, res) => {
  // 通过 req.query 客户端发送到服务器的数据
  const query = req.query;
  console.log(query, 'query')
  res.send({
    code: 0, // 0: 请求成功  -1: 请求失败
    msg: 'GET请求成功', // 请求的状态描述
    data: query, // 服务器像客户端返回数据
  });
});

在这里插入图片描述

实现POST请求

方式1:form-data,Express默认不会解析form-data,因为它通常用于文件上传,需要额外的处理。你可以使用multer这个中间件来处理multipart/form-data(也就是form-data)类型的请求。multer是专门为Express设计的,用于处理多部分/表单数据,这包括上传文件。

// 设置multer存储选项(这里只是演示,实际上你可能需要配置磁盘存储或其他选项)
const storage = multer.memoryStorage(); 
const upload = multer({ storage: storage });
// 处理POST请求
router.post('/upload', upload.single('file'), (req, res) => {
   // 通过 req.query 客户端发送到服务器的数据
   const body = req.body;
   console.log(body, 'body')
   res.send({
     code: 0, // 0: 请求成功  -1: 请求失败
     msg: 'POST请求成功', // 请求的状态描述
     data: body, // 服务器像客户端返回数据
   });
});

在这里插入图片描述

方式2:urlencoded,想要获取url-encoded请求体的数据,需要引入对应的中间件。

// 配置解析表单数据的中间件
app.use(express.urlencoded({extended: false}))
// 处理POST请求
router.post('/post', (req, res) => {
  // 通过 req.query 客户端发送到服务器的数据
  const body = req.body;
  console.log(body, 'body')
  res.send({
    code: 0, // 0: 请求成功  -1: 请求失败
    msg: 'POST请求成功', // 请求的状态描述
    data: body, // 服务器像客户端返回数据
  });
});

在这里插入图片描述

全部代码

index.js

const express = require('express');
const indexRouter = require('./router'); // 引入路由
const app = express();
const port = 3000;
// 配置解析表单数据的中间件
app.use(express.urlencoded({extended: false}))
// 挂载路由
app.use('/api', indexRouter);
// 启动服务器
app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

router.js

const express = require('express');
const multer = require('multer');
const router = express.Router();
// 设置multer存储选项(这里只是演示,实际上你可能需要配置磁盘存储或其他选项)
const storage = multer.memoryStorage(); // 使用内存存储,适用于小文件或不需要持久化的场景
const upload = multer({ storage: storage });
// 处理GET请求
router.get('/get', (req, res) => {
  // 通过 req.query 客户端发送到服务器的数据
  const query = req.query;
  console.log(query, 'query')
  res.send({
    code: 0, // 0: 请求成功  -1: 请求失败
    msg: 'GET请求成功', // 请求的状态描述
    data: query, // 服务器像客户端返回数据
  });
});
// 处理POST请求
router.post('/upload', upload.single('file'), (req, res) => {
   // 通过 req.query 客户端发送到服务器的数据
   const body = req.body;
   console.log(body, 'body')
   res.send({
     code: 0, // 0: 请求成功  -1: 请求失败
     msg: 'POST请求成功', // 请求的状态描述
     data: body, // 服务器像客户端返回数据
   });
});
// 处理POST请求
router.post('/post', (req, res) => {
  // 通过 req.query 客户端发送到服务器的数据
  const body = req.body;
  console.log(body, 'body')
  res.send({
    code: 0, // 0: 请求成功  -1: 请求失败
    msg: 'POST请求成功', // 请求的状态描述
    data: body, // 服务器像客户端返回数据
  });
});
module.exports = router;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小灰灰学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值