13

I am going through the book Web Development with Node and Express and have hit a snag.

I was instructed to put the below in my application file, but it looks like body-parser is deprecated and will not work. How can I achieve the same functionality?

This is my current code:

app.use(require('body-parser')());

app.get('/newsletter', function(req, res){

    // we will learn about CSRF later...for now, we just
    // provide a dummy value
    res.render('newsletter', { csrf: 'CSRF token goes here' });
});

app.post('/process', function(req, res){

    console.log('Form (from querystring): ' + req.query.form); 
    console.log('CSRF token (from hidden form field): ' + req.body._csrf); 
    console.log('Name (from visible form field): ' + req.body.name); 
    console.log('Email (from visible form field): ' + req.body.email); res.redirect(303, '/thank-you');
});
Nic
  • 6,211
  • 10
  • 46
  • 69
Lance Hietpas
  • 351
  • 1
  • 5
  • 17
  • possible duplicate of [bodyParser is deprecated express 4](http://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4) – Ben Fortune May 08 '15 at 14:52

3 Answers3

20

Just wanted to update this thread because I tried the solution above and received undefined. Express 4.16+ has implemented their own version of body-parser so you do not need to add the dependency to your project. You can run it natively in express as follows:

app.use(express.json()); // Used to parse JSON bodies
app.use(express.urlencoded()); // Parse URL-encoded bodies using query-string library
// or
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded bodies using qs library

Source: Express JS — body-parser and why may not need it

See also: query-string vs qs

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
JLee365
  • 241
  • 2
  • 8
8

From: bodyParser is deprecated express 4

It means that using the bodyParser() constructor has been deprecated, as of 2014-06-19.

app.use(bodyParser()); //Now deprecated

You now need to call the methods separately

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

app.use(bodyParser.urlencoded());

app.use(bodyParser.json());

And so on.

Community
  • 1
  • 1
Alex Alksne
  • 528
  • 5
  • 13
  • When I replace app.use(require('body-parser')()); with app.use(bodyParser.urlencoded()); app.use(bodyParser.json()); I get body parser not defined. – Lance Hietpas May 08 '15 at 14:29
4

Do not use body-parser anymore

Since Express 4.16+ the body parsing functionality has become builtin with express

So, you can simply do

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

from directly express, without having to install body-parser.

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



And to get the POST data content, you can use req.body

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

    var postData = req.body;

    //Or if body comes as string,

    var postData = JSON.parse(req.body);
});
Abraham
  • 12,140
  • 4
  • 56
  • 92