Search code examples
node.jsexpressmiddlewarebody-parser

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?

Solution

  • 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/