bodyParser is deprecated in Express 4: What to do?

If you are watching old tutorials or reading some old documentation, you see the usage of a module called body-parser to parse the body of incoming HTTP requests. Its ability to convert raw data (like JSON, form data, etc.) into a JavaScript object and store it in req.body, made it super popular.

It was so useful in those days that even big companies started using it and with so much popularity, Express 4.16.0 integrated its core functionality and we no longer have to rely on it. As a result, body-parser was deprecated after Express 4.16.0.

So if now with the latest version of express, you are trying to use body-parser, you might get different types of errors related to body-parser deprecated. So let’s see how to solve this by using the built-in Express parsing method.

Note: The body-parser isn’t completely removed from npm, it’s still available but deprecated in favour of the Express built-in methods.

Click here to follow our exclusive 2024 Next.js series!

The Old Way: Using body-parser in Express (Before 4.16.0)

For Express version less than 4.16.0, we can still use the body-parser package in the following way to parse the body of incoming HTTP request

Installation of body-parser

To use body-parser, we have to install it using npm:

npm install body-parser

Implementation

Here’s how we can implement it in our Express application:

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

const app = express();
const port = 3000;

// Parse JSON request bodies
app.use(bodyParser.json());

// Parse URL-encoded request bodies
app.use(bodyParser.urlencoded({ extended: true }));

// Define a route that handles POST requests
app.post('/api/data', (req, res) => {
  // Access the parsed data from the request body
  const jsonData = req.body;

  // Process the data or respond as needed
  res.json({ message: 'Data received successfully', data: jsonData });
});

// Start the server
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

The New Way: Using Built-in Body Parsing in Express (4.16.0 and Later)

As we learned, starting with Express.js version 4.16.0, the body-parser middleware has been deprecated and is no longer necessary because its functionality has been integrated into Express itself.

You can now use express.json() and express.urlencoded() methods to handle request data directly.

What is express.json()?

The express.json() is used to parse the JSON (JavaScript Object Notation) data sent in the request body.

const express = require('express');
const app = express();

// Middleware to parse JSON request bodies
app.use(express.json());

// route handling code here

When a request comes to the Express server with a JSON body, express.json() automatically parses the JSON data and attaches it to the req.body property.

Example route:

app.post('/api/data', (req, res) => {
  const jsonData = req.body; // Access the parsed JSON data here
  //code to process the JSON data
});

The client must include the Content-Type: application/json header in the HTTP request. express.json() will only parse the body as JSON if this header is set.

Example request:

POST /api/data
Content-Type: application/json

{
  "key": "value"
}

What is express.urlencoded()?

The express.urlencoded is used to parse the URL-encoded data sent in the request body

const express = require('express');
const app = express();

// Middleware to parse URL-encoded request bodies
app.use(express.urlencoded({ extended: true }));

//route handling code here

When a request comes to the Express server with a URL-encoded data body, express.urlencoded() also automatically parses that data and attaches it to the req.body property.

Example route:

app.post('/api/data', (req, res) => {
  const formData = req.body; // Access the parsed URL-encoded data here
  // Your code to process the form data
});

The client must include the Content-Type: application/x-www-form-urlencoded header in the HTTP request. express.urlencoded() will only parse the body as URL-encoded data if this header is set.

Example request:

POST /api/data
Content-Type: application/x-www-form-urlencoded

key1=value1&key2=value2

Implementation

Below is the updated code, replacing the use of body-parser with the built-in middleware methods express.json() and express.urlencoded().

const express = require('express');

const app = express();
const port = 3000;

// Middleware to parse JSON request bodies
app.use(express.json());

// Middleware to parse URL-encoded request bodies
app.use(express.urlencoded({ extended: true }));

// Define a route that handles POST requests
app.post('/api/data', (req, res) => {
    // Access the parsed data from the request body
    const jsonData = req.body;

    // Process the data or respond as needed
    res.json({ message: 'Data received successfully', data: jsonData });
});

// Start the server
app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

Summary

In short, before Express 4.16.0, it was required to use the body-parser module for parsing incoming data. After Express 4.16.0, body-parser features were added to the Express and it became deprecated. Now it is recommended to use express.json() to parse JSON data and express.urlencoded() to handle URL-encoded data.

Click here to check out our in-depth guide on how to handle form data without using body-parser.

Reference

https://stackoverflow.com/questions/24330014/bodyparser-is-deprecated-express-4

Arnab Biswas
Arnab Biswas
Articles: 10