Express.js req.body undefined

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

    Most of the time req.body is undefined due to missing JSON parser

    const express = require('express');
    app.use(express.json());
    

    could be missing for the body-parser

    const bodyParser  = require('body-parser');
    app.use(bodyParser.urlencoded({extended: true}));
    

    and sometimes it's undefined due to cros origin so add them

    const cors = require('cors');
    app.use(cors())
    

提交回复
热议问题