json data received in node js post request is undefined code example
Example 1: req.body is undefined
var bodyParser = require('body-parser')
var app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
Example 2: express request body undefined
npm install body-parser
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.post('/login', urlencodedParser, function (req, res) {
res.send('welcome, ' + req.body.username)
})
app.post('/api/users', jsonParser, function (req, res) {
})