Introduction to body-parser in Node.js
What is body-parser?
body-parser is the Node.js body parsing middleware. Middleware are the functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle.In Node body-parser is used to parse the body of requests.
This provides the following parsers,
- JSON body parser
- Raw body parser
- Text body parser
- URL-encoded form body parser
body-parser does not handle multipart bodies. For this you will have to use some other like,
Installation
You can install body-parser by using the following code in your command line.
npm install body-parser
And then you have to require it in your app.js file
Example:
var bodyParser = require('body-parser');
//To parse URL encoded data
app.use(bodyParser.urlencoded({ extended: false }))
//To parse json data
app.use(bodyParser.json())
To get to know more about body-parser visit github.
Comments
Post a Comment