1Earlier versions of Express used to have a lot of middleware bundled with it. bodyParser was one of the middlewares that came it. When Express 4.0 was released they decided to remove the bundled middleware from Express and make them separate packages instead. The syntax then changed from app.use(express.json()) to app.use(bodyParser.json()) after installing the bodyParser module.
2
3bodyParser was added back to Express in release 4.16.0, because people wanted it bundled with Express like before. That means you don't have to use bodyParser.json() anymore if you are on the latest release. You can use express.json() instead.
4
5same for the app.use(express.urlencoded({ extended: true })) . you can use bodyparse.urlencoded
1const express = require('express')
2const bodyParser = require('body-parser')
3
4const app = express()
5
6// parse application/x-www-form-urlencoded
7app.use(bodyParser.urlencoded({ extended: false }))
8
9// parse application/json
10app.use(bodyParser.json())
11
12app.use(function (req, res) {
13 res.setHeader('Content-Type', 'text/plain')
14 res.write('you posted:\n')
15 res.end(JSON.stringify(req.body, null, 2))
16})
17
18