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...
For further actions, you may consider blocking this person and/or reporting abuse
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?
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.
As per the Express docs
It's good to remember that the body parsing was added, removed and then added again stackoverflow.com/a/47232318
Amazing! Didn’t know this! Thank you :)