ExpressJS Error: Body-Parser Deprecated

Richard WU picture Richard WU · May 28, 2015 · Viewed 21.3k times · Source

Question I'm trying build a Node.js API, when write my server.js file, my code looks like this:

var express = require('express');
var express = require('body-parser');

var app = express();
app.use(bodyParser.json());

app.get('/api/posts', function(req,res) {
res.json([
    {
        username: 'oscar',
        body: 'hello'
    }

])
})

app.listen(3000, function() {
console.log('Server Listening on', 3000)
})

However, in the command prompt I am getting this error:

body-parser deprecated bodyParser: use individual json.urlencoded
middlewares server.js:4:11
body-parser deprecated undefined extended: provide extended option
node_modules\body-parser\index.js:85:29

I tried changing this to :

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

and

app.use(bodyParser.urlencoded({ extended: false }))

like other posts suggest, but it still gives the same error. Not sure what to do now! Please help.

Thanks!

Answer

HDK picture HDK · May 28, 2015
var app = express();

// configure body-parser

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