Express.js req.body undefined

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

    express.bodyParser() needs to be told what type of content it is that it's parsing. Therefore, you need to make sure that when you're executing a POST request, that you're including the "Content-Type" header. Otherwise, bodyParser may not know what to do with the body of your POST request.

    If you're using curl to execute a POST request containing some JSON object in the body, it would look something like this:

    curl -X POST -H "Content-Type: application/json" -d @your_json_file http://localhost:xxxx/someRoute
    

    If using another method, just be sure to set that header field using whatever convention is appropriate.

提交回复
热议问题