Not deprecated alternative to body-parser in Express.js
I am going through the book Web Development with Node and Express and I got confused.
I was prompted to put below in my application file, but it looks like it is body-parser
outdated and won't 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');
});
+3
Lance hietpas
source
to share
1 answer
From: bodyParser is deprecated to express 4
This means that the use of the bodyParser () constructor has been deprecated as of 2014-06-19.
app.use(bodyParser()); //Now deprecated
Now you need to call the methods separately
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
Etc.
+3
Alex alksne
source
to share