2

I'm in the process of learning Node.js - and in working thru some examples, I'm using 'express' framework, and I've installed body-parser (using npm install body-parser) and it went fine... however when I start my app - Node shows this:

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

however it goes on to show its "normal" listening on port xxxx.

Of course, just learning - I don't have a ton of experience with packages, but I take the first line as 'express 4' not liking my version of body-parser - although I got it form the link on express' site.

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

My app js looks like this at the moment - AND IT'S WORKING, so I'm not sure how to 'take' this message. (the line with " app.use( bodyParser() );" is line 30 reference above)

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.' );
});

And the index.js from the installed package of body-parser looks like this:

/*!
 * 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);
        });
    }
}

this appears on line 29 - and I have to assume this is the source of the message

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

I don't understand the purpose though? As I said - things 'seem' to be working - With quite a few 'js warnings' in my console, but still.

I guess the question is. 1- is this type of message normal in node packages? 2- if not, what can be done 3- can someone familiar please give me some insight on where to find information.

Thank You.

Matteo Alessani
  • 10,264
  • 4
  • 40
  • 57
j-p
  • 3,698
  • 9
  • 50
  • 93

1 Answers1

10

well... maybe I should read the docs - instead of following a tutorials 'say so'...

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

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

seems one must explicitly state WHICH parse to use in the new version, where the tutorial (only done in May 2014) was showing that bodyParse() didn't care, and was smart enough to use JSON if it was a json object, and urlencoded if not - that apparently is not the case. or IS the case, in a deprecated fashion, to be unsupported in the future.

j-p
  • 3,698
  • 9
  • 50
  • 93