bodyParser 已弃用 express 4 [英] bodyParser is deprecated express 4

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

问题描述

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

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

body-parser 弃用 bodyParser:使用单独的 json/urlencoded 中间件 server.js:15:12body-parser 已弃用 urlencoded:明确指定extended: true"用于扩展解析 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);

推荐答案

表示使用bodyParser()构造函数已经已弃用,截至 2014 年 6 月 19 日.

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

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

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

You now need to call the methods separately

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

等等.

如果您仍然收到带有 urlencoded 的警告,您需要使用

If you're still getting a warning with urlencoded you need to use

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

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

The extended config object key now needs to be explicitly passed, since it now has no default value.

如果您使用的是 Express >= 4.16.0,正文解析器已在方法 express.json()express.urlencoded() 下重新添加.

If you are using Express >= 4.16.0, body parser has been re-added under the methods express.json() and express.urlencoded().

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

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