Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 9, 2020 02:33 am GMT

You probably don't need body-parser in your Express apps

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 -ynpm 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());

Original Link: https://dev.to/taylorbeeston/you-probably-don-t-need-body-parser-in-your-express-apps-3nio

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To