1//make sure it is in this order
2npm i body-parser
3
4const express = require('express')
5const bodyParser = require('body-parser')
6
7const app = express()
8
9// parse application/x-www-form-urlencoded
10app.use(bodyParser.urlencoded({ extended: false }))
11
12// parse application/json
13app.use(bodyParser.json())
14
15app.use(function (req, res) {
16 res.setHeader('Content-Type', 'text/plain')
17 res.write('you posted:\n')
18 res.end(JSON.stringify(req.body, null, 2))
19})
20
1const express = require('express')
2
3const app = express()
4
5app.use(express.json())
6app.use(express.urlencoded())
1var express = require('express')
2var bodyParser = require('body-parser')
3
4var app = express()
5
6// parse various different custom JSON types as JSON
7app.use(bodyParser.json({ type: 'application/*+json' }))
8
9// parse some custom thing into a Buffer
10app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
11
12// parse an HTML body into a string
13app.use(bodyParser.text({ type: 'text/html' }))
14
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