ExpressJS Error: Body-Parser Deprecated
Asked Answered
L

6

11

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!

Libertarian answered 28/5, 2015 at 4:25 Comment(0)
S
13
var app = express();

// configure body-parser

app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
Sternberg answered 28/5, 2015 at 7:12 Comment(0)
H
5

I had similar deprecated warning. Here's my original code:

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

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

Adding {extended: true} fixed the warning for me. Here's changed code:

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

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
Hoboken answered 1/1, 2016 at 9:27 Comment(0)
H
2

Unless you have a typo, change:

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

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

to:

var bodyParser   = require('body-parser');
var app = express();
app.use(bodyParser());

Do have a look at what the request and response objects do here

Hygienics answered 28/5, 2015 at 4:49 Comment(0)
G
2

You don't actually need the line app.use(bodyParser.urlencoded({extended: true}));

I've got it to work with the following code:

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

var app = express();

app.use(bodyParser.json());

As log as you have app.user(bodyParser.json()); you shouldn't get the error.

Gauldin answered 8/6, 2016 at 21:26 Comment(0)
M
2

You don't need body-parser module any more with current express.js versions (≥ 4.16).

Instead, use the json parser already included in express:

app.use(express.json())

Source: https://expressjs.com/en/changelog/4x.html#4.16.0

Methaemoglobin answered 27/5, 2021 at 16:2 Comment(0)
A
2

Don't use body-parser

In new versions of express, body parsing is now builtin. So, you can simply use

app.use(express.json()) //For JSON requests
app.use(express.urlencoded({extended: true}));

from directly express

You can uninstall body-parser using npm uninstall body-parser



Then you can simply get the POST content from req.body

app.post("/yourpath", (req, res)=>{

    var postData = req.body;

    //Or for string JSON body, you can use this

    var postData = JSON.parse(req.body);
});
Acarology answered 25/6, 2021 at 7:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.