我将其作为Express服务器的配置
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.set('view engine', 'ejs');
app.set("view options", { layout: true });
//Handles post requests
app.use(express.bodyParser());
//Handles put requests
app.use(express.methodOverride());
但是当我req.body.something
在路线上询问时,仍然会指出一些错误body is undefined
。这是使用的路线示例req.body
:
app.post('/admin', function(req, res){
console.log(req.body.name);
});
我读到这个问题是由于缺少引起的,app.use(express.bodyParser());
但是如您所见,我在路由之前将其称为。
有什么线索吗?
If you are using some external tool to make the request, make sure to add the header:
Content-Type: application/json