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
1// Express v4.16.0 and higher
2// --------------------------
3const express = require('express');
4
5app.use(express.json());
6app.use(express.urlencoded({
7 extended: true
8}));
9
10// For Express version less than 4.16.0
11// ------------------------------------
12const bodyParser = require('body-parser');
13
14app.use(bodyParser.json());
15app.use(bodyParser.urlencoded({
16 extended: true
17}));
1var express = require("express");
2var app = express();
3var bodyParser = require("body-parser");
4app.use(bodyParser.urlencoded({extended: true}));
1// Express/Connect top-level generic
2// This example demonstrates adding a generic JSON and URL-encoded parser as a top-level middleware, which will parse the bodies of all incoming requests.
3// This is the simplest setup.
4
5var express = require('express')
6var bodyParser = require('body-parser')
7var 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')
18res.end(JSON.stringify(req.body, null, 2))})