What is the difference between node's bodyParser and express's urlencoded middleware?
Asked Answered
P

1

14

After reading about both i still can't wrap my head about it, could be language differences bu please help clarify.

express.urlencoded()

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

And

and body-parser middleware

Parse incoming request bodies in a middleware before your handlers, available under the req.body property.

i understand that express.urlencoded is based on Nodejs body-parser. and both pages,

https://expressjs.com/en/api.html#express.urlencoded

and

https://expressjs.com/en/resources/middleware/body-parser.html

even say the same warning note:

As req.body’s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting. For example, req.body.foo.toString() may fail in multiple ways, for example foo may not be there or may not be a string, and toString may not be a function and instead a string or other user-input.

but eventually both give me a req.body with params sent in requests body object. so why should i use body-parser (which i have to install separatly) instead of always using express.urlencoded()

I know this isn't a code problem but i thank in advance for anyone who can list up to the main differences.

Peabody answered 15/4, 2019 at 15:4 Comment(0)
D
21

so why should i use body-parser (which i have to install separatly) instead of always using express.urlencoded()

For the simple reason that it was not available in older version of express

This middleware is available in Express v4.16.0 onwards.

If you're using the latest version, there's almost no reason to.

body-parser provides a few additional utilities like bodyParser.raw([options]) or bodyParser.text([options]) which almost nobody uses (never seen use one myself).

Diazonium answered 15/4, 2019 at 15:7 Comment(5)
Oh! Thank you for a smiple yet very undestandable answer! :D So i imagine that even-though i'm using the latest express version and there isn't really a reason to install body-parser there is still a change of dependencies fails (like what i'm experiencing with PassportJS which after investigating seems it misses the body parser even though i have the urlencoded middleware on. could be?)Peabody
passportjs have only 2 dependencies listed passport-strategy, pause, it could be from other packages depding on body-parserDiazonium
Great! Thanks i'll keep looking. but as i understand from your answer and comment. there could be dependencies failures. which brings up a reliability problem with urlencoded() for me and makes me wonder if it's just good to always include body-parser instead of urlencoded just to be safe always. I mean i'ts a node module after all. It's too reliable by it self. @naga - elixir - jar Thanks so much for the answer.Peabody
now express have body-parser as dependencies, so it should automatically download body-parser when you run npm install express. Not sure why you need to re-downloadDiazonium
It might just be that the body parser isn't really my problem in my code. it just led me to investigate about it and just couldn't tell the diff. :) i'll keep lookingPeabody

© 2022 - 2024 — McMap. All rights reserved.