DEV Community

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

Taylor Beeston on June 09, 2020

What is body-parser? Often, when I see a blog post or article describing an Express.js server, it usually starts out with something simi...
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?

Collapse
 
sumanthyedoti profile image
Sumanth Yedoti

I think, express.json() is to parse header body which is json. Body parser is also to parse form data. Please correct me if im wrong.

Collapse
 
taylorbeeston profile image
Taylor Beeston

As per the Express docs

This is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser.

Returns middleware that only parses JSON and only looks at requests where the Content-Type header matches the type option. This parser accepts any Unicode encoding of the body and supports automatic inflation of gzip and deflate encodings.

A new body object containing the parsed data is populated on the request object after the middleware (i.e. req.body), or an empty object ({}) if there was no body to parse, the Content-Type was not matched, or an error occurred.

Collapse
 
icyjoseph profile image
Joseph

It's good to remember that the body parsing was added, removed and then added again stackoverflow.com/a/47232318

Collapse
 
ste profile image
Ste

Amazing! Didn’t know this! Thank you :)