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
1var express = require('express')
2
3var app = express()
4
5app.use(express.json()) // for parsing application/json
6app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
7
8app.post('/profile', function (req, res, next) {
9 console.log(req.body)
10 res.json(req.body)
11})
12
1var bodyParser = require('body-parser')
2var app = express()
3
4// parse application/x-www-form-urlencoded
5app.use(bodyParser.urlencoded({ extended: false }))
6
7// parse application/json
8app.use(bodyParser.json())
9
1app.post('/login', (req, res) => {
2 console.log(req.body.email) // "user@example.com"
3 console.log(req.body.password) // "helloworld"
4})
1app.get('/user/:id', (req, res) => {
2 console.log(req.params.id) // "1234562134654"
3})
4or
5app.get('/user/:user_id', (req, res) => {
6 console.log(req.params.user_id) // "1234562134654"
7})