body-parser节点模块返回“弃用”消息

我在学习Node.js的过程中 – 通过一些例子,我使用“快速”框架,并且我已经安装了body-parser(使用npm install body-parser),并且它很好。 。但是当我启动我的应用程序 – 节点显示这个:

body-parser deprecated bodyParser: use individual json/urlencoded middlewares: app.js:30:11 body-parser deprecated undefined extended: provide extended option: node_modules\body_parser\index.js:85:29 

但它继续显示其在端口xxxx上的“正常”侦听。

当然,只是学习 – 我没有大量的软件包经验,但我把第一行写成“express 4”,不喜欢我的版本的body-parser–尽pipe我在express网站上获得了它的链接。

 http://expressjs.com/resources/middleware.html https://github.com/expressjs/body-parser?_ga=1.200820398.1885847446.1420349783 

我的应用程序js现在看起来就像这样 – 而且它的工作,所以我不知道如何“采取”这个消息。 (带“app.use(bodyParser());”的行是上面第30行的引用)

 var express = require( 'express' ); var path = require( 'path' ); var bodyParser = require( 'body-parser' ); var app = express(); // configure app app.set( 'view engine', 'ejs' ); app.set( 'views', path.join( __dirname, 'views' ) ); // use middleware // Body Parser for express app.use( bodyParser() ); // ****** this is line 30 referenced in the msg above ***** // ** NOTE this data is here for our learning scenario - but we'd normally be calling a persistentan datastore (such as a db) to get the data var todoItems = [ { id: 1, desc: 'one' } ,{ id: 2, desc: 'two' } ,{ id: 3, desc: 'three' } ]; // define routes app.get( '/', function( req, res ) { res.render( 'index', { title: 'My 1st Node App' ,items: todoItems }); }); app.post( '/add', function( req, res ) { // handle the post data (need middleware (body_parser) to handle 'body'..express does not come with default ) var newItem = req.body.newItem; //debug purposes console.log( newItem ); // do something with the data - *NOTE: normally we'd put it to a persistent datastore todoItems.push({ id: todoItems.length + 1 ,desc: newItem }); // redirect the user res.redirect( '/' ); }); app.listen( 1337, function() { console.log( 'ready on Port 1337.' ); }); 

从安装的body-parser包中得到的index.js如下所示:

 /*! * body-parser * Copyright(c) 2014 Douglas Christopher Wilson * MIT Licensed */ /** * Module dependencies. */ var deprecate = require('depd')('body-parser') var fs = require('fs') var path = require('path') /** * @typedef Parsers * @type {function} * @property {function} json * @property {function} raw * @property {function} text * @property {function} urlencoded */ /** * Module exports. * @type {Parsers} */ exports = module.exports = deprecate.function(bodyParser, 'bodyParser: use individual json/urlencoded middlewares') /** * Path to the parser modules. */ var parsersDir = path.join(__dirname, 'lib', 'types') /** * Auto-load bundled parsers with getters. */ fs.readdirSync(parsersDir).forEach(function onfilename(filename) { if (!/\.js$/.test(filename)) return var loc = path.resolve(parsersDir, filename) var mod var name = path.basename(filename, '.js') function load() { if (mod) { return mod } return mod = require(loc) } Object.defineProperty(exports, name, { configurable: true, enumerable: true, get: load }) }) /** * Create a middleware to parse json and urlencoded bodies. * * @param {object} [options] * @return {function} * @deprecated * @api public */ function bodyParser(options){ var opts = {} options = options || {} // exclude type option for (var prop in options) { if ('type' !== prop) { opts[prop] = options[prop] } } var _urlencoded = exports.urlencoded(opts) var _json = exports.json(opts) return function bodyParser(req, res, next) { _json(req, res, function(err){ if (err) return next(err); _urlencoded(req, res, next); }); } } 

这出现在第29行 – 我不得不认为这是消息的来源

 exports = module.exports = deprecate.function(bodyParser, 'bodyParser: use individual json/urlencoded middlewares') 

我不明白目的吗? 正如我所说 – 事情似乎是在工作 – 在我的控制台有不less“js警告”,但仍然。

我想问题是。 1-这种types的消息在节点包中是否正常? 2-如果没有,可以做什么3-可以有人熟悉,请给我一些洞察力,在哪里可以find信息。

谢谢。

好吧…也许我应该阅读文档 – 而不是遵循教程“这么说”…

 // parse application/x-www-form-urlencoded app.use(bodyParser.urlencoded({ extended: false })) // parse application/json app.use(bodyParser.json()) 

似乎必须明确指出哪些parsing才能在新版本中使用,其中教程(仅在2014年5月完成)显示bodyParse()不在意,并且足够聪明,如果它是json对象就可以使用JSON, urlencoded如果不是 – 显然不是这样的。 或者以不赞成的方式,将来是不被支持的。