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?
-
87in order to read HTTP POST data , we have to use "body-parser" node module. body-parser is a piece of express middleware that reads a form's input and stores it as a javascript object accessible through `req.body` – refactor Jul 11 '16 at 12:19
-
6With express you can read any data inside HTTP request, such as headers `req.headers` (array), you can read the body of the http packet as `req.body` explained by @CleanCrispCode and you can read as query parameter `req.query.variable`, It helps since express automatically transforms the request in javascript objects – Fernando Zamperin Jul 11 '16 at 17:54
-
9@refactor -- this might be *one* of the many reasons we *have to use* body parser, but it doesn't say what it does, i.e. that HTTP request and response objects are streams and that they're not 'readable' as single object like ``res.body`` without the entire stream being buffered into ``res.body`` first. – ortonomy May 03 '18 at 02:32
-
8With Express version 4.16+ they have included their own version of body-parser built in so you don't have to pull in this package. – StefanBob Mar 03 '20 at 02:16
-
1Also see [You don't need body-parser in Express 4.16+](https://stackoverflow.com/a/63999686/11667949) – Shivam Jha Sep 21 '20 at 20:48
11 Answers
Edit: in 2019-april-2 in express@4.16.0 the body-parser middleware is included in express, so you don't need to install body-parser separately anymore. for more details see this
OLD:
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

- 1
- 1

- 4,505
- 1
- 14
- 14
-
210This is quite possibly the lamest thing ever. Why would Express core devs make it incredibly difficult for newcomers to get on board by making them install additional middleware for the most common use cases in web development? – elmt Apr 05 '18 at 18:34
-
7
-
Do `app.use(bodyParser.urlencoded({ extended: true }))` and `app.use(bodyParser.json())` automatically invoke `next()`? – user1063287 May 15 '18 at 02:32
-
@elmt I guess you can´t wait much more from people who writes such awful, incomprehensible for newcomers documentation – May 27 '18 at 18:37
-
2@user1063287 yes it does. `urlencoded()` and `json()` are actually middleware factories that return a middleware function which invokes `next()` – Nick Manning Jun 14 '18 at 16:50
-
3It is not lame @elmt, node is not only for web, it can be used on desktop, mobile, etc, and in these cases it is not a required module. Node can adapt to your application without any liability – fnaquira Jul 12 '18 at 19:42
-
45
-
1
-
5@elmt actually this is something that is happening to other frameworks such as react native too! and there is a good reason for it. we should try to lighten the core framework as much as possible. this way, someone that needs a specific functionality can easily add it to the project and the one who doesn't need it can have the lightest version of his app – Reza Shoja Jun 02 '19 at 15:53
-
@elmt has a point. Why not just include that within express rather than keeping it as a separate middleware? Also, body-parser doesn't seem to work as a standalone library which you can use in apps written in non-express frameworks/vanilla nodeJs apps so I'm not sure what they've achieved by doing this. – Saifur Rahman Mohsin Aug 12 '19 at 14:50
-
-
1
-
2@elmt Express version 4.16+ include their own body-parser implementation in the default Express package so there is no need for you to download another dependency. – Mohamed Ben HEnda Feb 07 '21 at 22:15
-
@MohamedBenHEnda - i'm confused, the [official Express docs on body-parser](https://expressjs.com/en/resources/middleware/body-parser.html) don't mention anything about `body-parser` being bundled with `Express`, and they provide usage instructions as `npm install body-parser` and `var bodyParser = require('body-parser')`. if this is not correct, and you don't need to install anything 'extra', how do you call `body-parser` and use it? – user1063287 Nov 21 '21 at 07:40
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 data
app.use(bodyParser.json());
//support parsing of application/x-www-form-urlencoded post data
app.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");
});

- 7,487
- 4
- 42
- 75
-
4Hey thanks for the info, can you post a code example without body parser? – Ilyas karim Oct 23 '19 at 14:06
-
1@llyas you can check some blog https://itnext.io/how-to-handle-the-post-request-body-in-node-js-without-using-a-framework-cd2038b93190. Here they have use http module of node.js , same way you can use in express also , inside `app.post("/posturl",function(req,res,next){` – Himanshu sharma Aug 24 '20 at 04:14
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.

- 1
- 1

- 4,463
- 28
- 39
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.
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, and requesting 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.

- 5,753
- 72
- 57
- 129

- 722
- 5
- 4
In order to get access to the post data we have to use body-parser
. Basically what the body-parser
is which allows express to read the body and then parse that into a Json
object that we can understand.

- 703
- 11
- 31

- 167
- 1
- 3
Understanding Requests Body
When receiving a POST or PUT request, the request body might be important to your application. Getting at the body data is a little more involved than accessing request headers. The request object that's passed in to a handler implements the ReadableStream interface. This stream can be listened to or piped elsewhere just like any other stream. We can grab the data right out of the stream by listening to the stream's 'data' and 'end' events.
The chunk emitted in each 'data' event is a Buffer. If you know it's going to be string data, the best thing to do is collect the data in an array, then at the 'end', concatenate and stringify it.
let body = []; request.on('data', (chunk) => { body.push(chunk); }).on('end', () => { body = Buffer.concat(body).toString(); // at this point, `body` has the entire request body stored in it as a string });
Understanding body-parser
As per its documentation
Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
As you saw in the first example, we had to parse the incoming request stream manually to extract the body. This becomes a tad tedious when there are multiple form data of different types. So we use the body-parser package which does all this task under the hood.
It provides four modules to parse different types of data
After having the raw content body-parser will use one of the above strategies(depending on middleware you decided to use) to parse the data. You can read more about them by reading their documentation.
After setting the req.body
to the parsed body, body-parser will invoke next()
to call the next middleware down the stack, which can then access the request data without having to think about how to unzip and parse it.

- 879
- 9
- 15
History:
Earlier versions of Express used to have a lot of middleware bundled with it. bodyParser
was one of the middleware that came with it. When Express 4.0 was released they decided to remove the bundled middleware from Express and make them separate packages instead. The syntax then changed from app.use(express.json())
to app.use(bodyParser.json())
after installing the bodyParser
module.
bodyParser
was added back to Express in release 4.16.0, because people wanted it bundled with Express like before. That means you don't have to use bodyParser.json()
anymore if you are on the latest release. You can use express.json()
instead.
The release history for 4.16.0 is here for those who are interested, and the pull request is here.
Okay, back to the point,
Implementation:
All you need to add is just add,
app.use(express.json());
app.use(express.urlencoded({ extended: true}));
Before route declaration, instead of,
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
And Express will take care of your request. :)
Full example will looks like,
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true}));
app.post('/test-url', (req, res) => {
console.log(req.body)
return res.send("went well")
})
app.listen(3000, () => {
console.log("running on port 3000")
})

- 4,345
- 3
- 26
- 40
If you don't want to use a separate npm package body-parser
, the latest express (4.16+) has the body-parser middleware built-in that can be used like this:
const app = express();
app.use(express.json({ limit: '100mb' }));
p.s. Not all functionalities of body-parser
are present in the new express.json()
middleware. Refer to the documentation for full usage here

- 1,938
- 3
- 24
- 33

- 906
- 11
- 22
It parses the HTTP request body. This is usually necessary when you need to know more than just the URL you hit, particular in the context of a POST or PUT PATCH HTTP request where the information you want is contains in the body.
Basically its a middleware for parsing JSON, plain text, or just returning a raw Buffer object for you to deal with as you require.

- 384
- 1
- 8
- 23
These are all a matter of convenience.
Basically, if the question were 'Do we need to use body-parser
?' The answer is 'No'. We can come up with the same information from the client-post-request using a more circuitous route that will generally be less flexible and will increase the amount of code we have to write to get the same information.
This is kind of the same as asking 'Do we need to use express
to begin with?' Again, the answer there is no, and again, really it all comes down to saving us the hassle of writing more code to do the basic things that express comes with 'built-in'.
On the surface - body-parser
makes it easier to get at the information contained in client requests in a variety of formats instead of making you capture the raw data streams and figuring out what format the information is in, much less manually parsing that information into useable data.

- 1,190
- 11
- 11
Keep it simple :
- if you used
post
request so you will need thebody
of the request, so you will needbody-parser
. - No need to install body-parser with
express
, but you have touse
it if you will receive post request.
app.use(bodyParser.urlencoded({ extended: false }));
{ extended: false }
false meaning, you do not have nested data inside your body object. Note that: the request data embedded within the request as a body Object.

- 2,390
- 3
- 30
- 39