kelsk
kelsk

Reputation: 138

express.json() vs body-parser

I'm a junior developer in Node.js. I've been importing "body-parser" for Express apps, but I recently discovered that Express has built-in middleware based on body-parser since v4.16.0.

I did RTFM for body-parser and Express, but the documentation looks pretty much the same to me. Hoping more experienced devs can offer an opinion:

  1. Is there a significant difference between body-parser and the equivalent built-in Express functions, i.e. express.json()?
  2. Is there ever a reason to import body-parser instead of using the parsing functions already in Express?

Upvotes: 4

Views: 7608

Answers (2)

InoY
InoY

Reputation: 847

For those who, like me, come here looking for actual middleware for JSON parsing:

express.json is literally body-parser. In the code of Express you can find this code (lib/express.js):

var bodyParser = require('body-parser')
// …
exports.json = bodyParser.json

Link: github/express

So it doesn't matter which one use for parsing

Upvotes: -1

Kasey Chang
Kasey Chang

Reputation: 542

The reason this is separate is because express.js used NOT to have any body parsing functions. That was added in V4.16+

If you are running a later version, you technically no longer need body-parser.

See https://medium.com/@mmajdanski/express-body-parser-and-why-may-not-need-it-335803cd048c

also see https://codewithhugo.com/parse-express-json-form-body/

Upvotes: 5

Related Questions