Express.js req.body undefined

前端 未结 30 2755
半阙折子戏
半阙折子戏 2020-11-22 12:02

I have this as configuration of my Express server

app.use(app.router); 
app.use(express.cookieParser());
app.use(express.session({ secret: \"keyboard cat\" }         


        
30条回答
  •  情深已故
    2020-11-22 12:44

    Looks like the body-parser is no longer shipped with express. We may have to install it separately.

    var express    = require('express')
    var bodyParser = require('body-parser')
    var app = express()
    
    // parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: false }))
    
    // parse application/json
    app.use(bodyParser.json())
    
    // parse application/vnd.api+json as json
    app.use(bodyParser.json({ type: 'application/vnd.api+json' }))
    app.use(function (req, res, next) {
    console.log(req.body) // populated!
    

    Refer to the git page https://github.com/expressjs/body-parser for more info and examples.

提交回复
热议问题