javascript Nodejs - 使用 express 4.9.0 在帖子中未定义 Req.body
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27485735/
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
Nodejs - Req.body undefined in post with express 4.9.0
提问by Tyrion
I'm a beginner of nodejs, I try to know req.body using a middleware body-parse or using nothing, but both found req.body is undefined. Here is my code
我是 nodejs 的初学者,我尝试使用中间件 body-parse 或什么都不使用来了解 req.body,但两者都发现 req.body 未定义。这是我的代码
var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer');
app.get('/', function(req, res) {
res.send("Hello world!\n");
});
app.post('/module', function(req, res) {
console.log(req);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(multer);
console.log(req.body);
});
app.listen(3000);
module.exports = app;
And I use command curl -X POST -d 'test case' http://127.0.0.1:3000/module
to test it.
我使用命令curl -X POST -d 'test case' http://127.0.0.1:3000/module
来测试它。
express's version: 4.9.0
node's version: v0.10.33
express 版本:4.9.0
节点版本:v0.10.33
Please help, thank you.
请帮忙,谢谢。
采纳答案by mscdex
By default cURL uses Content-Type: application/x-www-form-urlencoded
for form submissions that do not contain files.
默认情况下,cURLContent-Type: application/x-www-form-urlencoded
用于不包含文件的表单提交。
For urlencoded forms your data needs to be in the right format: curl -X POST -d 'foo=bar&baz=bla' http://127.0.0.1:3000/module
or curl -X POST -d 'foo=bar' -d 'baz=bla' http://127.0.0.1:3000/module
.
对于 urlencoded 表单,您的数据需要采用正确的格式:curl -X POST -d 'foo=bar&baz=bla' http://127.0.0.1:3000/module
或curl -X POST -d 'foo=bar' -d 'baz=bla' http://127.0.0.1:3000/module
.
For JSON, you'd have to explicitly set the right Content-Type
: curl -H "Content-Type: application/json" -d '{"foo":"bar","baz":"bla"}' http://127.0.0.1:3000/module
.
对于 JSON,您必须明确设置正确的Content-Type
: curl -H "Content-Type: application/json" -d '{"foo":"bar","baz":"bla"}' http://127.0.0.1:3000/module
。
Also as @Brett noted, you need to app.use()
your middleware beforethat POST route somewhere (outside of a route handler).
同样正如@Brett 指出的那样,您需要在POST 路由之前的某个地方(路由处理程序之外)使用app.use()
中间件。
回答by Brett
You are placing the express configuration for body-parser
in the wrong location.
您将快速配置放置body-parser
在错误的位置。
var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer');
// these statements config express to use these modules, and only need to be run once
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(multer);
// set up your routes
app.get('/', function(req, res) {
res.send("Hello world!\n");
});
app.post('/module', function(req, res) {
console.log(req);
console.log(req.body);
});
app.listen(3000);
module.exports = app;
回答by Muhammad Soliman
You must make sure that you define all express configurations before defining routes. as body-parser is responsible for parsing requst's body.
您必须确保在定义路由之前定义所有快速配置。因为 body-parser 负责解析请求的正文。
var express = require('express'),
app = express(),
port = parseInt(process.env.PORT, 10) || 8080;
//you can remove the app.configure at all in case of it is not supported
//directly call the inner code
app.configure(function(){
app.use(bodyParser.urlencoded());
//in case you are sending json objects
app.use(bodyParser.json());
app.use(app.router);
});
app.listen(port);
app.post("/module", function(req, res) {
res.send(req.body);
});