What does body-parser do with express? What does body-parser do with express? express express

What does body-parser do with express?


To handle HTTP POST requests in Express.js version 4 and above, you need to install the middleware module called body-parser.

body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body.

The middleware was a part of Express.js earlier but now you have to install it separately.

This body-parser module parses the JSON, buffer, string and URL encoded data submitted using HTTP POST request. Install body-parser using NPM as shown below.

npm install body-parser --save

edit in 2019-april-2:in express@4.16.0 the body-parser middleware bundled with express. for more details see this


Yes we can work without body-parser. When you don't use that you get the raw request, and your body and headers are not in the root object of request parameter . You will have to individually manipulate all the fields.

Or you can use body-parser, as the express team is maintaining it .

What body-parser can do for you: It simplifies the request.
How to use it: Here is example:

Install npm install body-parser --save

This how to use body-parser in express:

const express = require('express'),      app = express(),      bodyParser = require('body-parser');// support parsing of application/json type post dataapp.use(bodyParser.json());//support parsing of application/x-www-form-urlencoded post dataapp.use(bodyParser.urlencoded({ extended: true }));

Link.

https://github.com/expressjs/body-parser.

And then you can get body and headers in root request object . Example

app.post("/posturl",function(req,res,next){    console.log(req.body);    res.send("response");});


The answer here explain it very detailed and brilliantly, the answer contains:

In short; body-parser extracts the entire body portion of an incoming request stream and exposes it on req.body as something easier to interface with. You don't need it per se, because you could do all of that yourself. However, it will most likely do what you want and save you the trouble.


To go a little more in depth; body-parser gives you a middleware which uses nodejs/zlib to unzip the incoming request data if it's zipped and stream-utils/raw-body to await the full, raw contents of the request body before "parsing it" (this means that if you weren't going to use the request body, you just wasted some time).

After having the raw contents, body-parser will parse it using one of four strategies, depending on the specific middleware you decided to use:

  • bodyParser.raw(): Doesn't actually parse the body, but just exposes the buffered up contents from before in a Buffer on req.body.

  • bodyParser.text(): Reads the buffer as plain text and exposes the resulting string on req.body.

  • bodyParser.urlencoded(): Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST) and exposes the resulting object (containing the keys and values) on req.body. For comparison; in PHP all of this is automatically done and exposed in $_POST.

  • bodyParser.json(): Parses the text as JSON and exposes the resulting object on req.body.

Only after setting the req.body to the desirable contents will it call the next middleware in the stack, which can then access the request data without having to think about how to unzip and parse it.

You can refer to body-parser github to read their documentation, it contains information regarding its working.