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
Followed by the classic
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
// more express stuff
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());
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());
Top comments (21)
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 🙏
No problem!
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
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
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.
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
It might still be better to just use
express.json
or to rename it to something likejsonMiddleware
thoughExpress also has built-in middleware for raw data (
express.raw
) and text (express.text
) since 4.17.0.That is true! They've also got
express.urlencoded
,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.
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 ?
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.That's so cool! I just tested it, thanks!
No problem! Glad you found it useful!
What I understood is that the parser in express is actually the same one, since they always are used together, they just integrated it.
Thanks. This was helpful.
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.