Be the first user to complete this post
|
Add to List |
Access the request body of a post request in your nodejs - expressjs app.
body
on the request object that you can then make use of in your route handling function.
The Packages
As of this writing, in express 4.x, you gotta do the following.
Install the body-parser package in your dependencies
npm install body-parser --save
The Application Code
If you have an app.js or an index.js which acts as the starting point for your application, include the following lines in your middleware before you define your route handling functions.
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
myRouter.post('/somePattern',function(req, res, next){
console.log(req.body);
});
Also Read:
- Send email using nodejs and express in 5 simple steps
- Testing promise sequence using mocha, chai, chai-as-promised, sinon
- gzip compress and cache api response in express
- Accessing the request body and session in nodejs using express
- Setup passportjs for local authentication and authorization using expressjs