本文翻译自:bodyParser is deprecated express 4
I am using express 4.0 and I'm aware that body parser has been taken out of the express core, I am using the recommended replacement, however I am getting 我正在使用快递4.0,我知道身体解析器已被取出快递核心,我使用推荐的替代品,但我得到了
body-parser deprecated bodyParser: use individual json/urlencoded middlewares server.js:15:12 body-parser deprecated urlencoded: explicitly specify "extended: true" for extended parsing node_modules/body-parser/index.js:74:29
Where do I find this supposed middlewares? 我在哪里可以找到这个假设的中间件? or should I not be getting this error? 或者我不应该收到此错误?
var express = require('express');
var server = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var passport = require('./config/passport');
var routes = require('./routes');
mongoose.connect('mongodb://localhost/myapp', function(err) {
if(err) throw err;
});
server.set('view engine', 'jade');
server.set('views', __dirname + '/views');
server.use(bodyParser());
server.use(passport.initialize());
// Application Level Routes
routes(server, passport);
server.use(express.static(__dirname + '/public'));
server.listen(3000);
#1楼
参考:https://stackoom.com/question/1e5La/bodyParser已弃用快递
#2楼
It means that using the bodyParser() constructor has been deprecated , as of 2014-06-19. 这意味着自2014-06-19起不再使用bodyParser() 构造函数 。
app.use(bodyParser()); //Now deprecated
You now need to call the methods separately 您现在需要单独调用这些方法
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
And so on. 等等。
If you're still getting a warning with urlencoded you need to use 如果您仍然收到urlencoded警告,则需要使用
app.use(bodyParser.urlencoded({
extended: true
}));
The extended config object key now needs to be explicitly passed, since it now has no default value. 现在需要显式传递extended配置对象键,因为它现在没有默认值。
If you are using Express >= 4.16.0, body parser has been re-added under the methods express.json() and express.urlencoded() . 如果您使用Express> = 4.16.0,则在express.json()和express.urlencoded()方法下重新添加了body解析器。
#3楼
Want zero warnings ? 想要零警告吗? Use it like this: 像这样使用它:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
Explanation : The default value of the extended option has been deprecated, meaning you need to explicitly pass true or false value. Explanation :不推荐使用extended选项的默认值,这意味着您需要显式传递true或false值。
#4楼
In older versions of express, we had to use: 在旧版本的express中,我们不得不使用:
app.use(express.bodyparser());
because body-parser was a middleware between node and express. 因为body-parser是node和express之间的中间件。 Now we have to use it like: 现在我们必须使用它:
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
#5楼
I found that while adding 我发现在添加时
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
helps, sometimes it's a matter of your querying that determines how express handles it. 帮助,有时这是你的查询的问题,决定了如何处理它。
For instance, it could be that your parameters are passed in the URL rather than in the body 例如,您的参数可能是在URL中而不是在正文中传递的
In such a case, you need to capture both the body and url parameters and use whichever is available (with preference for the body parameters in the case below) 在这种情况下,您需要捕获body和url参数并使用可用的任何一个(在下面的情况下优先选择body参数)
app.route('/echo')
.all((req,res)=>{
let pars = (Object.keys(req.body).length > 0)?req.body:req.query;
res.send(pars);
});
#6楼
body-parser is a piece of express middleware that reads a form's input and stores it as a javascript object accessible through
req.body'body-parser' must be installed (vianpm install --save body-parser) For more info see: https://github.com/expressjs/body-parser body-parser是一段快速中间件,它读取表单的输入并将其存储为可通过req.body访问的javascript对象。必须安装body-parser'(通过npm install --save body-parser)有关详细信息,请参阅: https://github.com/expressjs/body-parser
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
When extended is set to true, then deflated (compressed) bodies will be inflated; 当extended设置为true时,放气(压缩)的物体将膨胀; when extended is set to false, deflated bodies are rejected. 当extended被设置为false时,被放气的身体被拒绝。
本文探讨了在Express.js 4.0及更高版本中body-parser被弃用的原因,提供了更新后的中间件使用方法,包括如何正确配置body-parser以避免警告,并解释了在Express 4.16.0及以上版本中如何使用内置的express.json()和express.urlencoded()。
4257

被折叠的 条评论
为什么被折叠?



