DEV Community

Taylor Beeston
Taylor Beeston

Posted on

Body Parser You probably don't need body-parser in your Express apps

What is body-parser?

Often, when I see a blog post or article describing an Express.js server, it usually starts out with something similar to the following:

npm init -y
npm i express body-parser
Enter fullscreen mode Exit fullscreen mode

Followed by the classic

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

const app = express();
app.use(bodyParser.json());
// more express stuff
Enter fullscreen mode Exit fullscreen mode

I used to have these four lines of code in practically every Express app I've ever made!

However, a few weeks ago I was poring over the Express Docs and noticed that as of version 4.16.0 (which came out three years ago!), Express basically comes with body-parser out of the box!

How do I use the Express version?

Well, you can pretty much just search bodyParser, and replace it with express!

This means our four lines of code above can be refactored into the following three lines of code:

const express = require('express');

const app = express();
app.use(express.json());
Enter fullscreen mode Exit fullscreen mode

If you are using Babel (which I would highly recommend!), you can even use a named import to make the code even more concise:

import express, { json } from 'express';

const app = express();
app.use(json());
Enter fullscreen mode Exit fullscreen mode

Top comments (21)

Collapse
 
halasproject profile image
Salah Bentayeb

it proves that the majority prefer to follow the tutorials than to read the documentation ... I am part of the majority ๐Ÿ‘€, I just learned this technique thanks for sharing ๐Ÿ™

Collapse
 
taylorbeeston profile image
Taylor Beeston

No problem!

Collapse
 
technolaaji profile image
Mohamad Kalaaji

It has been there for quite some time ๐Ÿ™ƒ body parser became part of express

You can use both, they perform the exact same job but one thing you need to keep in mind that if there is an issue with the express build in body parser then you have to update express as whole, as for the independent body parser package if there is an issue with it then you have to update that package only

Collapse
 
taylorbeeston profile image
Taylor Beeston

That is a good point that I hadn't thought of. Though at least mostly in my limited experience, express upgrades have been pretty painless

Collapse
 
jaterlwj profile image
Jater Loh

Hi, thanks for sharing! I was not aware that express ships with bodyParser now, I have to give this a try. In regards to named imports, it does make the code more concise. But I think it may be a tad confusing to have a function named json.

Collapse
 
taylorbeeston profile image
Taylor Beeston

At first I thought it would be weird too, but I've found that only using it on routes that actually need to parse json actually doesn't look so bad. For example

import { Router, json } from 'express';

const router = Router();

router.route('/').get((_, response) => {
  return response.status(200).send("No parsing needed here");
});

router.route('/').post(json(), (request, response) => {
  return response.status(200).json(request.body);
});

export default router;
Enter fullscreen mode Exit fullscreen mode

It might still be better to just use express.json or to rename it to something like jsonMiddleware though

Collapse
 
tobiassn profile image
Tobias SN

Express also has built-in middleware for raw data (express.raw) and text (express.text) since 4.17.0.

Collapse
 
taylorbeeston profile image
Taylor Beeston

That is true! They've also got express.urlencoded,

Collapse
 
ingosteinke profile image
Ingo Steinke, web developer

Thanks for your post! When following tutorials, even if they do not seem outdated at first sight, once I ask myself: do I really have to do this now in 202... (add current year) chances are, I don't.

No Babel, no manual HTTP CORS headers, and no external body parser.

Collapse
 
wolfgangving profile image
Yuri Silva

Yes, I was searching ways of deploy a express API in Firebase and every tutorial I saw was saying to install body-parser framework, those which are from 2021/22, which made me think: WHY are they keeping using it ? Does express body parser performs bad or is outshined by the other one ?

Collapse
 
gabuardi profile image
Josuรฉ Gabuardi

Now read the NPM documentation... since now you don't need Babel... you only need the attribute type: module in your package.json and node.js will use ESM imports by default.

Collapse
 
dayesouza profile image
Day Souza

That's so cool! I just tested it, thanks!

Collapse
 
taylorbeeston profile image
Taylor Beeston

No problem! Glad you found it useful!

Collapse
 
goudekettingrm profile image
Robin Goudeketting

What I understood is that the parser in express is actually the same one, since they always are used together, they just integrated it.

Collapse
 
geekreflex profile image
Jerry Nwosu

Thanks. This was helpful.

Collapse
 
aryasheikhi profile image
Arya

Excuse me if my question seems amateur, but why doesn't express parse the body automatically and needs to be told so?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.