Express.js req.body undefined

前端 未结 30 2928
半阙折子戏
半阙折子戏 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:29

    Latest versions of Express (4.x) has unbundled the middleware from the core framework. If you need body parser, you need to install it separately

    npm install body-parser --save
    

    and then do this in your code

    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())
    

提交回复
热议问题