Express.js req.body未定义 [英] Express.js req.body undefined

查看:380
本文介绍了Express.js req.body未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将其作为我的Express服务器的配置

I have this as configuration of my Express server

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 在我的路线我得到一些错误指出正文未定义。以下是使用 req.body 的路线示例:

But still when I ask for req.body.something in my routes I get some error pointing out that body is undefined. Here is an example of a route that uses req.body :

app.post('/admin', function(req, res){
    console.log(req.body.name);
});

我读到这个问题是由于缺少 app.use express.bodyParser()); ,但是您可以看到我在路线之前打电话。

I read that this problem is caused by the lack of app.use(express.bodyParser()); but as you can see I call it before the routes.

任何线索?

推荐答案

已经发布在一个评论下,我使用

As already posted under one comment, I solved it using

app.use(require('connect').bodyParser());

而不是

app.use(express.bodyParser());

我还不知道为什么简单的 express.bodyParser() / code>不工作...

I still don't know why the simple express.bodyParser() is not working...

这篇关于Express.js req.body未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆