javascript - 主体解析器 Node 模块返回 'deprecated' 消息

标签 javascript node.js express body-parser

<分区>

我正在学习 Node.js - 并通过一些示例工作,我正在使用“快速”框架,并且我已经安装了 body-parser(使用 npm install body-parser)并且它成功了很好......但是当我启动我的应用程序时 - Node 显示:

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 版本——尽管我从 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')

虽然我不明白目的是什么?正如我所说 - 事情“似乎”在起作用 - 我的控制台中有很多“js 警告”,但仍然如此。

我想问题是。 1-这种类型的消息在 Node 包中是否正常? 2-如果没有,可以做什么 3- 熟悉的人可以告诉我在哪里可以找到信息吗?

谢谢。

最佳答案

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

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

// parse application/json
app.use(bodyParser.json())

似乎必须明确说明要在新版本中使用哪个解析,其中教程(仅在 2014 年 5 月完成)显示 bodyParse() 不关心,并且足够聪明地使用 JSON 如果它是 json对象,如果不是,则进行 urlencoded - 显然不是这样。或者是这种情况,以一种过时的方式,在未来不受支持。

关于javascript - 主体解析器 Node 模块返回 'deprecated' 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27763638/

相关文章:

node.js - 如何获取express router中包含特殊字符的router值

javascript - CSS Bootstrap 关闭模态并转到链接

javascript - ui-select 将数组元素显示为下拉列表中的选项

javascript - 在无状态组件上添加默认 Props

php - 使用 PHP 和 Node.js 的 Websocket

node.js - 在我的 Node js 中获取 Windows 用户 ID,以便我可以针对 LDAP 进行身份验证

javascript - 使用 Express 模块作为中间件

javascript - NodeJS 从 mongo 添加数字

node.js - 使用 Express.Router() 具有特定中间件的快速路由

javascript - 如何让 jQuery 更新remove() 上的内部元素计数器?