node.js Express js 正文解析器不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7451966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Express js body parser not working?
提问by Jesse
I have the following in my node server using express (truncated to the important parts):
我使用 express 在我的节点服务器中有以下内容(截断到重要部分):
var app = express.createServer();
app.all(/test/,function(req,res){
console.log(req.headers);
console.log(req.body);
res.send("");
});
function appStart(cobrands){
app.configure(function(){
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use('/min',express.static('../min'));
app.use('/js',express.static('../js'));
app.use('/css',express.static('../css'));
app.use('/img',express.static('../img'));
});
app.listen(8080);
}
I then have a simple form that calls out to localhost:8080 like so:
然后我有一个简单的表单,可以像这样调用 localhost:8080:
<form action="http://localhost:8080/test" method="post">
<input type="hidden" name="test" value="testing"/>
<input type="submit" name="submit" value="to node"/>
</form>
But express.bodyParser doesn't seem to be doing anything, and req.body is undefined. Here's the output of the console.logs:
但是 express.bodyParser 似乎没有做任何事情,并且 req.body 是未定义的。这是console.logs的输出:
// req.headers
{ host: 'localhost:8080',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1) AppleWebKit/534.48.3 (KHTML, like Gecko) Version/5.1 Safari/534.48.3',
'content-length': '27',
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
origin: 'file://',
'content-type': 'application/x-www-form-urlencoded',
'accept-language': 'en-us',
'accept-encoding': 'gzip, deflate',
cookie: '',
connection: 'keep-alive' }
// req.body
undefined
Note: content-typeis correctly defined as application/x-www-form-urlencodedas it should be for bodyParser to work, and I've verified that it's coming over by popping open the debug tools in Safari and verifying that the form data is present.
注意:content-type正确定义application/x-www-form-urlencoded为 bodyParser 工作应该是这样,我已经通过在 Safari 中弹出打开调试工具并验证表单数据是否存在来验证它是否会出现。
回答by Ryan Olds
http://expressjs.com/guide.html#configuration
http://expressjs.com/guide.html#configuration
Note the use of app.router, which can (optionally) be used to mount the application routes, otherwise the first call to app.get(), app.post(), etc will mount the routes.
注意 app.router 的使用,它可以(可选)用于挂载应用程序路由,否则第一次调用 app.get()、app.post() 等将挂载路由。
What's happening is you're calling app.all() before adding any of the other middleware. Doing this effectively puts app.router in front of all of your other middleware, resulting in them never being used on requests that end inside your routes.
发生的事情是您在添加任何其他中间件之前调用 app.all() 。这样做有效地将 app.router 置于您所有其他中间件的前面,导致它们永远不会用于以您的路由结束的请求。
Mounting the application routes is pretty much the same as doing app.use(app.router);.
挂载应用程序路由与执行app.use(app.router);.
In the end your stack is looks this:
最后你的堆栈看起来是这样的:
app.use(app.router); // Contains your /test/ route
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use('/min',express.static('../min'));
app.use('/js',express.static('../js'));
app.use('/css',express.static('../css'));
app.use('/img',express.static('../img'));
tl;dr Move your route between your call to app.configure() and app.listen().
tl;dr 在调用 app.configure() 和 app.listen() 之间移动您的路线。
回答by Szymon Wygnański
I had similar problem and I figured out that this is becouse broken/missing express.bodyParser(). Instead express's bodyParser i've used connect's bodyParser and it worked:
我有类似的问题,我发现这是因为 express.bodyParser() 损坏/丢失。相反,我使用了连接的 bodyParser 的 express 的 bodyParser 并且它起作用了:
app.use(require('connect').bodyParser());

