node.js中实现GET&POST请求

创建基本的服务器

javascript 复制代码
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}`);
});

创建路由文件

javascript 复制代码
const express = require('express');
const router = express.Router();

module.exports = router;

实现GET请求

javascript 复制代码
// 处理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设计的,用于处理多部分/表单数据,这包括上传文件。

javascript 复制代码
// 设置multer存储选项(这里只是演示,实际上你可能需要配置磁盘存储或其他选项)
const storage = multer.memoryStorage(); 
const upload = multer({ storage: storage });
javascript 复制代码
// 处理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请求体的数据,需要引入对应的中间件。

javascript 复制代码
// 配置解析表单数据的中间件
app.use(express.urlencoded({extended: false}))
javascript 复制代码
// 处理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

javascript 复制代码
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

javascript 复制代码
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;
相关推荐
天天扭码6 分钟前
🔥 别再用 class 了!JS 原型链才是 YYDS
前端·javascript·面试
GISer_Jinger11 分钟前
📢《告别手动抓狂!Trae国际版+BrowserTools MCP 实现前端错误调试自动化》🚀
前端
前端大白话12 分钟前
震惊!90%前端工程师都踩过的坑!computed属性vs methods到底该怎么选?一文揭秘高效开发密码
前端·vue.js·设计模式
一天睡25小时12 分钟前
React与Vue表单的对比差异
前端·javascript
作曲家种太阳12 分钟前
第七章 响应式的 watch 实现【手摸手带你实现一个vue3】
前端
前端小巷子15 分钟前
深入解析 iframe
前端
WEI_Gaot15 分钟前
ES6 模板字符串
前端·javascript
前端大白话15 分钟前
前端工程师必看!手把手教你用CSS实现超丝滑的自适应视频嵌入
前端·css·前端框架
前端大白话15 分钟前
前端必看!figure标签在响应式图片排版中的王炸操作,grid/flex布局实战指南
前端·设计模式·html
前端大白话17 分钟前
深入理解 JavaScript 中 async 函数与 await 关键字的执行奥秘
前端·javascript·架构