我正在使用express 4.0,我知道主体解析器已经从express核心中取出,我正在使用推荐的替换,但我正在获得

bodyParser:使用单独的json/urlencoded中间件server.js:15:12 对扩展解析显式指定"extended: true" node_modules/ Body-parser /index.js:74:29

我在哪里可以找到这些所谓的中间件?或者我不应该得到这个错误?

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

当前回答

不要使用体解析器

如果你使用的是Express 4.16+,你可以用Express这样做:

app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads

你现在可以使用npm uninstall body-parser卸载body-parser

要获得POST内容,可以使用req.body

app.post("/yourpath", (req, res)=>{

    var postData = req.body;

    //Or if this doesn't work

    var postData = JSON.parse(req.body);
});

我希望这对你们有帮助

其他回答

如果你使用express > 4.16,你可以使用express.json()和express.urlencoded()

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

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

用这个,

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

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

就变成了,

const express = require('express');

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

这意味着使用bodyParser()构造函数已被弃用,截至2014-06-19。

app.use(bodyParser()); //Now deprecated

现在需要分别调用这些方法

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

等等。

如果你仍然得到一个urlencoded警告,你需要使用

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

扩展配置对象键现在需要显式传递,因为它现在没有默认值。

如果您正在使用Express >= 4.16.0,则在Express .json()和Express .urlencoded()方法下重新添加了body解析器。

我是在加法的时候发现的

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

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

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

在这种情况下,您需要捕获正文参数和url参数,并使用任何可用的参数(在下面的情况中优先使用正文参数)

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

你对使用express-generator的意见是什么,它将生成框架项目开始,没有弃用的消息出现在你的日志中

执行此命令

npm install express-generator -g

现在,在Node项目文件夹中输入这个命令创建新的Express.js starter应用程序。

express node-express-app

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

然后进入新创建的项目目录,安装npm包并使用命令启动应用程序

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

体解析器是一个表达中间件 读取表单的输入并将其存储为javascript 通过req.body可访问的对象 '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

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