Skip to content

my req.body is undefined, i using expreess 4.x #5

Answered by balebomm
nguyenphucminh asked this question in Q&A
Discussion options

You must be logged in to vote

express.bodyParser() is no longer bundled as part of express. You need to install it separately before loading:


npm i body-parser

// then in your app
var express = require('express')
var bodyParser = require('body-parser')
 
var app = express()
 
// create application/json parser
var jsonParser = bodyParser.json()
 
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
 
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
  res.send('welcome, ' + req.body.username)
})
 
// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
  // create user in…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by nguyenphucminh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants