当前位置: 首页 > news >正文

body-parser 与 express.json()、express.urlencoded() 会冲突

说明

body-parser 与 express.json()

body-parserexpress.json() 主要用于解析请求体中的 JSON 数据

express.json()express.urlencoded() 已经内置了 body-parser 的功能,不需要再额外使用 body-parser

bodyParser.urlencoded({ extended: false })express.urlencoded({ extended: true })导致配置冲突

即:

app.use(express.json());
app.use(express.urlencoded({ extended: true }));     // 冲突
app.use(bodyParser.urlencoded({ extended: false })); // 冲突(删除)
app.use(bodyParser.json());                          // 删除

body-parser 相关的代码删除,直接使用 express 提供的解析方法:

app.use(express.json());  
app.use(express.urlencoded({ extended: true }));

body-parser

body-parser 是一个独立的中间件,专门用于解析 HTTP 请求体。它提供多种解析方式;

bodyParser.json() 解析 JSON 格式的数据
bodyParser.urlencoded({ extended: true }) 解析 application/x-www-form-urlencoded 数据
bodyParser.text() 解析文本格式数据
bodyParser.raw() 解析 Buffer 格式的数据

在 Express 4.x 之前,开发者需要单独安装 body-parser,然后在 Express 代码中使用:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/data', (req, res) => {
  console.log(req.body);
  res.send('Received');
});

app.listen(3000, () => console.log('Server running on port 3000'));

express.json()

Express 4.16.0 版本开始,express.json()express.urlencoded() 直接内置于 express,不再需要单独安装 body-parser

使用 express.json() 代替 bodyParser.json()

const express = require('express');

const app = express();
app.use(express.json());  // 解析 JSON 数据
app.use(express.urlencoded({ extended: true }));  // 解析 URL 编码数据

app.post('/data', (req, res) => {
  console.log(req.body);
  res.send('Received');
});

app.listen(3000, () => console.log('Server running on port 3000'));

body-parserexpress.json() 主要用于解析请求体中的 JSON 数据,区别如下:

1. body-parser

body-parser 是一个独立的中间件,专门用于解析 HTTP 请求体。它提供多种解析方式,例如:

  • bodyParser.json() 解析 JSON 格式的数据
  • bodyParser.urlencoded({ extended: true }) 解析 application/x-www-form-urlencoded 数据
  • bodyParser.text() 解析文本格式数据
  • bodyParser.raw() 解析 Buffer 格式的数据

在 Express 4.x 之前,开发者需要单独安装 body-parser

npm install body-parser

然后在 Express 代码中使用:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/data', (req, res) => {
  console.log(req.body);
  res.send('Received');
});

app.listen(3000, () => console.log('Server running on port 3000'));

3. 区别

特性body-parserexpress.json()
是否需要安装需要 (npm install body-parser)不需要,内置于 Express 4.16+
用法bodyParser.json()express.json()
功能提供 JSON、URL 编码、文本、原始数据解析仅提供 JSON 和 URL 编码解析
推荐使用仅在 Express 4.16 之前Express 4.16 及以上推荐使用

结论

  • 如果你使用的是 Express 4.16+,直接使用 express.json()express.urlencoded() 即可,无需 body-parser
  • 如果你需要 解析文本原始 Buffer 数据,仍然可以使用 body-parser.text()body-parser.raw()

在大多数情况下,express.json() 足够满足需求,推荐优先使用它。

相关文章:

  • 大数据学习(65)- Hue详解
  • 【后端】【django drf】Django DRF API 编写规范(程序设计规则)
  • 地理信息系统(ArcGIS)在水文水资源及水环境中的应用:空间数据管理‌、空间分析功能‌、‌可视化表达‌
  • ICLR 2025|DAMO开发者矩阵合作专场
  • 用sphinx-doc整理文档#2
  • µCOS-III从入门到精通 第十五章(µC/OS-III内存管理)
  • windows 下用docker 部署nginx
  • 蓝桥杯 3514子串简写
  • 【python】一文掌握 Conda 指令 (anaconda备忘清单)
  • 【每日学点HarmonyOS Next知识】防截屏、加载不同View、函数传参、加载中效果、沉浸式底部状态栏
  • 第八节:红黑树(初阶)
  • 容易混淆的概念:requests 库 和 Flask 的 request
  • 【3DMAX科研绘图】3DMAX晶体网络插件CrystalNet使用方法
  • 【VUE2】第四期——路由
  • JavaScript如何实现复制图片功能?
  • C语言【数据结构】:理解什么是数据结构和算法(启航)
  • 【从零开始学习计算机科学】数据库系统(七)并发控制技术
  • 【实战ES】实战 Elasticsearch:快速上手与深度实践-8.2.2成本优化与冷热数据分离
  • 什么是SpringCloud?为何要选择SpringCloud?
  • 计算机视觉|首次写入政府工作报告!这个科技新词“具身智能”到底是什么?
  • 重庆网站建设 制作 设计 优惠价/地推团队接单平台
  • 电影网页设计素材/南宁百度快速优化
  • 如何做公司的网站/百度下载免费安装到桌面
  • 展厅展台设计搭建/西安抖音seo
  • 软件系统网站建设/抖音seo排名系统
  • 建设银网官方网站/seo搜索引擎优化就业指导