express.json 与 bodyParser.json [英] express.json vs bodyParser.json

查看:39
本文介绍了express.json 与 bodyParser.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个相对较新的应用程序,并想知道我应该使用哪个:

I'm writing a relatively new app and was wondering which I should use:

express.json()

bodyParser.json()

我可以假设他们做同样的事情吗.

Can I assume they do the same thing.

我只想使用 express.json() 因为它已经内置了.

I would like to just use express.json() as it is built in already.

推荐答案

Express 的早期版本曾经捆绑了很多中间件.bodyParser 是它附带的中间件之一.当 Express 4.0 发布时,他们决定从 Express 中删除捆绑的中间件,而是将它们分开打包.安装 bodyParser 模块后,语法从 app.use(express.json()) 更改为 app.use(bodyParser.json()).

Earlier versions of Express used to have a lot of middleware bundled with it. bodyParser was one of the middleware that came with it. When Express 4.0 was released they decided to remove the bundled middleware from Express and make them separate packages instead. The syntax then changed from app.use(express.json()) to app.use(bodyParser.json()) after installing the bodyParser module.

bodyParser 在 4.16.0 版本中被重新添加到 Express 中,因为人们希望它像以前一样与 Express 捆绑在一起.这意味着如果您使用的是最新版本,则不必再使用 bodyParser.json().您可以使用 express.json() 代替.

bodyParser was added back to Express in release 4.16.0, because people wanted it bundled with Express like before. That means you don't have to use bodyParser.json() anymore if you are on the latest release. You can use express.json() instead.

4.16.0 的发布历史是这里有兴趣,拉取请求是这里.

The release history for 4.16.0 is here for those who are interested, and the pull request is here.

这篇关于express.json 与 bodyParser.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆