208

In my node app, I am using express. all works fine, But i am getting error in the cmd. I use all are updated modules...

my code :

var express = require('express');
var bodyParser = require('body-parser');
var jade = require('jade');
var app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));


app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded()); // to support URL-encoded bodies


app.get('/',function(req,res){
    res.render('index.jade');
});

app.get('/login',function(req,res){
    res.render('index.jade');
});

app.post('/login',function(req,res){
    console.log(req.body);
});

app.get('/signup',function(req,res){
    res.render('signup.jade');
});

var env = process.env.PORT || 3000;


app.listen(env, function(req, res){
    console.log('i am working!');
});

Error:

D:\myLogin>node app
body-parser deprecated undefined extended: provide extended option app.js:11:20 //why i am getting this?
i am working!
{ username: 'jbarif@gmail.com', password: 'pass' } // i am getting response

Can any help me to understand this issue please?

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247

12 Answers12

421

You have to explicitly set extended for bodyParser.urlencoded() since the default value is going to change in the next major version of body-parser. Example:

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

Since express 4.16.0, you can also do:

app.use(express.urlencoded({ extended: true }))
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • 6
    @SamarthAgarwal According to the [readme](https://github.com/expressjs/body-parser/tree/3f1b02247702c2966d33ee73610a98d54cf32fe8#bodyparserurlencodedoptions), it uses the `qs` module to parse the body which allows for a nested array like syntax to be parsed such as `test[foo][bar]=baz` (which becomes `{'test': {'foo': {'bar': 'baz'}}}`) – Bailey Parker Jul 11 '15 at 03:35
  • @BaileyParker is your comment related to the `extended:true` expression? I think that the expression is nested. – Timo Mar 27 '21 at 09:41
78

Attention: With express version => 4.16.0 the body-parser middleware was added back under the methods express.urlencoded() and express.json()

Which can be used as:

app.use(express.urlencoded({extended: true})); 
app.use(express.json());   
ambianBeing
  • 3,449
  • 2
  • 14
  • 25
Dhiral Kaniya
  • 1,941
  • 1
  • 19
  • 32
19

The error says you need to provide the extended option for the body-parser like so:

app.use(bodyParser.urlencoded({ extended: false }))
mfreitas
  • 2,395
  • 3
  • 29
  • 42
11

Don't use body-parser

It's now built-in with new versions of Express, you can access request body just like this only using express:

app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads

Hence, you can now uninstall body-parser using npm uninstall body-parser



To get the POST content, you can use req.body

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

    var postData = req.body;

    //Or if this doesn't work

    var postData = JSON.parse(req.body);
});
Dharman
  • 30,962
  • 25
  • 85
  • 135
Abraham
  • 12,140
  • 4
  • 56
  • 92
7

As from Express version 4.16.0, you're expected to pass in extended property inside the bodyParser.urlencoded()

//  parse JSON-encoded bodies and URL-encoded bodies
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

See npm.js documentation page for sample: https://www.npmjs.com/package/body-parser#expressconnect-top-level-generic

If you're using Node v16.xx.x and "express": "^4.17.x" and above, there is no need again to use bodyPerser. It is now incorporated with express by default. Simply do the following below:

//  parse JSON-encoded bodies and URL-encoded bodies
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
tMan44wiz
  • 126
  • 1
  • 5
7

If you facing 'bodyParser' is deprecated.

Just do

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

Note: If you are using 4.16.0 or later express version.

Happy Patel
  • 2,259
  • 1
  • 16
  • 25
1

If you're using Node v16.xx.x and "express": "^4.17.x", there is no need again to use bodyPerser. It is now incoporated with Express by default. Simply do the following below:

//  parse JSON-encoded bodies and URL-encoded bodies
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
tMan44wiz
  • 126
  • 1
  • 5
1

If you are using node version v13.12.0:

app.use(express.urlencoded({ extended: true }))
Lee Goddard
  • 10,680
  • 4
  • 46
  • 63
  • Today's date has no bearing on what version of Node someone is using. – Ghostrydr Feb 19 '21 at 05:11
  • This is the only approach that works. Sadly, I saw it in very few blog posts, forums, etc., and the amount of confusion regarding this is through the roof! – ankush981 Mar 17 '21 at 09:31
1

You don`t need "body-parser" explicitly installed now. This will work

app.use(express.json());
Amit Baderia
  • 4,454
  • 3
  • 27
  • 19
0

if you are here after christmas from 2020 you just have to put the midlawares in order before your express declaretion and after the routes declarationenter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
Dario Coronel
  • 119
  • 2
  • 10
0

You don't need to use body-parser anymore.

In the new version of express.js, this option of parsing is available.

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
-1

Set limit 50 MB for avoid data handling error., In urlencode limit 50mb is for help you to pass imageData throw url

  app.use(bodyParser.json({
        limit : '50mb'    ///////// LIMIT for JSON
      }));

    app.use(bodyParser.urlencoded({
        limit : '50mb', ///////// LIMIT for URL ENCODE (image data)
        extended : true
      }));
Gowtham Sooryaraj
  • 3,639
  • 3
  • 13
  • 22