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;
相关推荐
祈澈菇凉1 小时前
Webpack的基本功能有哪些
前端·javascript·vue.js
小纯洁w1 小时前
Webpack 的 require.context 和 Vite 的 import.meta.glob 的详细介绍和使用
前端·webpack·node.js
想睡好2 小时前
css文本属性
前端·css
qianmoQ2 小时前
第三章:组件开发实战 - 第五节 - Tailwind CSS 响应式导航栏实现
前端·css
zhoupenghui1682 小时前
golang时间相关函数总结
服务器·前端·golang·time
White graces2 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
庸俗今天不摸鱼2 小时前
Canvas进阶-4、边界检测(流光,鼠标拖尾)
开发语言·前端·javascript·计算机外设
熬夜不洗澡2 小时前
Node.js中不支持require和import两种导入模块的混用
node.js
bubusa~>_<3 小时前
解决npm install 出现error,比如:ERR_SSL_CIPHER_OPERATION_FAILED
前端·npm·node.js
流烟默3 小时前
vue和微信小程序处理markdown格式数据
前端·vue.js·微信小程序