node.js bodyParser 已弃用 express 4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24330014/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:30:49  来源:igfitidea点击:

bodyParser is deprecated express 4

node.jsexpressmiddleware

提问by Melbourne2991

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

我正在使用 express 4.0 并且我知道主体解析器已从 express 核心中取出,我正在使用推荐的替代品,但是我得到了

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

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);

回答by Ben Fortune

It means that using the bodyParser()constructorhas been deprecated, as of 2014-06-19.

这意味着从2014 年 6 月 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 urlencodedyou need to use

如果您仍然收到警告,则urlencoded需要使用

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

The extendedconfig 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,则在 methodsexpress.json()和下重新添加了正文解析器express.urlencoded()

回答by smajl

Want zero warnings? Use it like this:

想要零警告?像这样使用它:

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

Explanation: The default value of the extendedoption has been deprecated, meaning you need to explicitly pass true or false value.

说明:该extended选项的默认值已被弃用,这意味着您需要显式传递 true 或 false 值。

回答by himanshu yadav

In older versions of express, we had to use:

在较旧版本的 express 中,我们必须使用:

app.use(express.bodyparser()); 

because body-parser was a middleware between node and express. Now we have to use it like:

因为 body-parser 是 node 和 express 之间的中间件。现在我们必须像这样使用它:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

回答by Parth Raval

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 (via npm install --save body-parser) For more info see: https://github.com/expressjs/body-parser

body-parser 是一个快速中间件,它读取表单的输入并将其存储为 javascript 对象,可通过req.body“body-parser”访问必须安装(通过npm install --save body-parser)有关更多信息,请参阅:https: //github.com/expressjs/body -解析器

   var bodyParser = require('body-parser');
   app.use(bodyParser.json()); // support json encoded bodies
   app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

When extendedis set to true, then deflated (compressed) bodies will be inflated; when extendedis set to false, deflated bodies are rejected.

extended设置为 true 时,则放气(压缩)的物体将被充气;当extended设置为false,瘪的身体将被拒绝。

回答by Sridhar

If you're using express > 4.16, you can use express.json()and express.urlencoded()

如果您使用 express > 4.16,则可以使用express.json()express.urlencoded()

The express.json()and express.urlencoded()middleware have been added to provide request body parsing support out-of-the-box. This uses the expressjs/body-parsermodule module underneath, so apps that are currently requiring the module separately can switch to the built-in parsers.

express.json()express.urlencoded()中间件已被添加到提供请求主体的解析支持外的开箱。这使用expressjs/body-parser下面的模块模块,因此当前需要单独使用模块的应用程序可以切换到内置解析器。

SourceExpress 4.16.0 - Release date: 2017-09-28

Source Express 4.16.0 - 发布日期:2017-09-28

With this,

有了这个,

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

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

becomes,

变成,

const express = require('express');

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

回答by Basheer AL-MOMANI

What is your opinion to use express-generator it will generate skeleton project to start with, without deprecated messagesappeared in your log

你对使用 express-generator 有什么看法,它会生成骨架项目开始,without deprecated messages出现在你的日志中

run this command

运行这个命令

npm install express-generator -g

Now, create new Express.js starter application by type this command in your Node projects folder.

现在,通过在your Node projects folder.

express node-express-app

That command tell express to generate new Node.js application with the name node-express-app.

该命令告诉 express 生成名为 的新 Node.js 应用程序node-express-app

then Go to the newly created project directory, install npm packagesand start the appusing the command

然后Go to the newly created project directoryinstall npm packagesstart the app使用命令

cd node-express-app && npm install && npm start

回答by Ian Mbae

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.

有帮助,有时您的查询决定了 express 如何处理它。

For instance, it could be that your parameters are passed in the URLrather than in the body

例如,您的参数可能是在URL中传递而不是在正文中传递

In such a case, you need to capture both the bodyand urlparameters and use whichever is available (with preference for the body parameters in the case below)

在这种情况下,您需要同时捕获bodyurl参数并使用可用的任何一个(在下面的情况下优先使用 body 参数)

app.route('/echo')
    .all((req,res)=>{
        let pars = (Object.keys(req.body).length > 0)?req.body:req.query;
        res.send(pars);
    });

回答by Mehedi Abdullah

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

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

I have the same problem but this work for me. You can try this extended part.

我有同样的问题,但这对我有用。你可以试试这个扩展部分。