express.json 与 bodyParser.json
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47232187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
express.json vs bodyParser.json
提问by Mika Sundland
I'm writing a relatively new app and was wondering which I should use:
我正在编写一个相对较新的应用程序,并想知道我应该使用哪个:
express.json()
or
或者
bodyParser.json()
Can I assume they do the same thing.
我可以假设他们做同样的事情。
I would like to just use express.json()as it is built in already.
我想使用,express.json()因为它已经内置了。
回答by Mika Sundland
Earlier versions of Express used to have a lot of middleware bundled with it. bodyParser was one of the middlewares that came 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.
早期版本的 Express 曾经捆绑了很多中间件。bodyParser 是它的中间件之一。当 Express 4.0 发布时,他们决定从 Express 中删除捆绑的中间件,而是将它们分开打包。安装 bodyParser 模块后,语法从app.use(express.json())变为app.use(bodyParser.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.
bodyParser 在 4.16.0 版本中被重新添加到 Express 中,因为人们希望它像以前一样与 Express 捆绑在一起。这意味着bodyParser.json()如果您使用的是最新版本,则不必再使用。你可以express.json()改用。
The release history is for 4.16.0 is herefor those who are interested, and the pull request here.
回答by Oleg Mikhailenko
YES! Correct
是的!正确的
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
回答by Shersha Fn
Yes both are same .
是的,两者都是一样的。
if you go into the file node_module/express/lib/express.js
如果你进入文件 node_module/express/lib/express.js
you can see under module dependencies body parser module is already imported
您可以在模块依赖项下看到主体解析器模块已导入
/**
* Module dependencies.
*/
var bodyParser = require('body-parser')
//other modules
the objects and methods inside bodyparser module are accessible as they are exported using the special object module.exports
bodyparser 模块中的对象和方法可以访问,因为它们是使用特殊对象 module.exports 导出的
exports = module.exports = createApplication;
exports.json = bodyParser.json
this is accessible from express object just by calling
只需通过调用即可从 express 对象访问 this
express.json()

