By default, Express 4.x or later doesn’t come with anything to parse request bodies. Therefore, we need to add something to do this.
In this article, we’ll look at how to use the body-parser middleware to do this with JSON and raw bodies.
Adding the Body-Parser Package
body-parser is a Node package that we can add onto our Express app to parse request bodies.
It doesn’t support multipart bodies, so we have to use other middleware packages to parse those.
However, it can parse JSON bodies, raw request bodies, text, and URL-encoded bodies.
To install the package, we run:
npm install body-parser
Then we can include by adding:
const bodyParser = require('body-parser');
Parsing JSON Bodies
We can parse JSON bodies by calling the JSON method. It takes an optional object with a few properties.
The options include:
- inflate— compressed request bodies will be inflated when this is set to- true. Otherwise, they’ll be rejected.
- limit— controls the maximum request body size. If it’s number, then it’s measured in bytes. If it’s a string then it can be parsed into a number of bytes.
- reviver— this is a function that’s passed into- JSON.parseas the second argument to map values to what we want
- strict— only accepts objects and arrays when set to- true. Otherwise, it’ll accept anything that- JSON.parseaccepts. Defaults to- true.
- type— this is used to determine what media type it’ll parse. It can be a string, array of strings or a function. If it’s not a function, then it’s directly passed into the- type-islibrary. Otherwise, the request is parsed if the data type the function is called with returns a truthy value
- verify— this is a function with signature- (req, res, buf, encoding), where- bufis a Buffer object of the raw request body. The parsing can be aborted by throwing an error in the function.
For example, we can use it as follows:
const express = require('express');  
const bodyParser = require('body-parser');  
const app = express();  
const options = {  
  inflate: true,  
  limit: 1000,  
  reviver: (key, value) => {  
    if (key === 'age') {  
      if (value < 50) {  
        return 'young'  
      }  
      else {  
        return 'old';  
      }  
    }  
    else {  
      return value;  
    }  
  }  
};  
app.use(bodyParser.json(options));
app.post('/', (req, res) => {  
  res.send(req.body);  
});
app.listen(3000);
Then when we make a POST request to / , we get back:
{  
    "name": "foo",  
    "age": "young"  
}
as the response since we check the age field in the reviver function and return 'young' or 'old' depending on the value . Otherwise, we return the value as-is.
The request body is parsed and set as the value of req.body .
Parsing Raw Bodies
We can parse raw bodies as a buffer. It supports the automatic inflation of gzip and deflate encodings.
The parsed body containing the parsed data is populated on the request object, i.e. it’ll be set as the value of req.body .
It takes an optional option object that can take the following properties:
- inflate— compressed request bodies will be inflated when this is set to- true. Otherwise, they’ll be rejected.
- limit— controls the maximum request body size. If it’s number, then it’s measured in bytes. If it’s a string then it can be parsed into a number of bytes.
- type— this is used to determine what media type it’ll parse. It can be a string, array of strings or a function. If it’s not a function, then it’s directly passed into the- type-islibrary. Otherwise, the request is parsed if the data type the function is called with returns a truthy value
- verify— this is a function with signature- (req, res, buf, encoding), where- bufis a Buffer object of the raw request body. The parsing can be aborted by throwing an error in the function.
For example, we can use it as follows:
const express = require('express');  
const bodyParser = require('body-parser');  
const app = express();  
const options = {  
  inflate: true,  
  limit: 1000,  
  type: 'text/plain'  
};  
app.use(bodyParser.raw(options));
app.post('/', (req, res) => {  
  res.send(req.body);  
});
app.listen(3000);
Then when we send a POST request to / with the body foo , we get back foo as the response.
This is because we specified text/plain as the type to parse the raw data.
It also smart enough to parse multiple body types. We can do that by passing in an array of type strings as follows:
const express = require('express');  
const bodyParser = require('body-parser');  
const app = express();  
const options = {  
  inflate: true,  
  limit: 1000,  
  type: ['text/plain', 'text/html']  
};  
app.use(bodyParser.raw(options));
app.post('/', (req, res) => {  
  res.send(req.body);  
});
app.listen(3000);
Then when we make a POST request to / with the body <b>foo</b> . Then we get back <b>foo</b> .
Conclusion
We can use the body-parser middleware to parse JSON and raw text bodies.
It also takes a variety of options to let us control whether to inflate compressed request bodies, map JSON values to something else, limit the size of a request body, and so on.
For raw request bodies, we can use body-parser to specify the type that the data is so we can parse it to that type of object and set it to req.body .
