What does body-parser do with express?

Tutorial: What does body-parser do with express?

I don’t understand why we need body-parser in an Express application, as we can get data without using body-parser. And what does it do actually and how?

Solutions of What does body-parser do with express?

Solution 1: What does body-parser do with express?

Edit: in 2019-April-2 in [email protected] the body-parser middleware is included in express, so you don’t need to install body-parser separately anymore. for more details see this


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

Solution 2: What does body-parser do with express?

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 the request parameter. You will have to individually manipulate all the fields.

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

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

Install npm install body-parser --save

This is how to use body-parser in express:

const express = require('express'),
      app = express(),
      bodyParser = require('body-parser');

// support parsing of application/json type post data

//support parsing of application/x-www-form-urlencoded post data
app.use(bodyParser.urlencoded({ extended: true }));



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


Solution 3: What does body-parser do with express?

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 that 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.

Solution 4: What does body-parser do with express?

Let’s try to keep this least technical.

Let’s say you are sending a html form data to node-js server i.e. you made a request to the server. The server file would receive your request under a request object. Now by logic, if you console log this request object in your server file you should see your form data some where in it, which could be extracted then, but whoa ! you actually don’t !

So, where is our data? How will we extract it if its not only present in my request.

A simple explanation to this is HTTP sends your form data in bits and pieces which are intended to get assembled as they reach their destination. So how would you extract your data.

But, why take this pain of every time manually parsing your data for chunks and assembling it. Use something called “body-parser” which would do this for you.

body-parser parses your request and converts it into a format from which you can easily extract relevant information that you may need.

For example, let’s say you have a sign-up form at your frontend. You are filling it, out and requesting the server to save the details somewhere.

Extracting username and password from your request goes as simple as below if you use body-parser.

var loginDetails = {    
    username : request.body.username,    
    password : request.body.password    

So basically, body-parser parsed your incoming request, assembled the chunks containing your form data, then created this body object for you and filled it with your form data.


I hope the What does body-parser do with express? the solution would be useful for you to learn something new from this solution. If it helped you then don’t forget to bookmark our site for more Quiz Answers and

Leave a Reply

Your email address will not be published.

error: Content is protected !!