Express:POST请求后,Req.body不确定

我在app.router之前使用了express.bodyParser(),头文件看起来是正确的,但是我仍然对req.body没有定义:

var app = express(); ... app.use(express.bodyParser()); ... app.use(app.router); 

req.headers的输出是这样的:

 { host: '127.0.0.1:3000', connection: 'keep-alive', 'content-length': '0', 'cache-control': 'max-age=0', accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', origin: 'http://127.0.0.1:3000', 'user-agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36', 'content-type': 'application/x-www-form-urlencoded', referer: 'http://127.0.0.1:3000/register', 'accept-encoding': 'gzip,deflate,sdch', 'accept-language': 'es-ES,es;q=0.8' } 

这个职位是这样宣布的:

 app.post('/register/do', function(req, res) { ... console.log(req.headers); console.log(req.body); ... }); 

我究竟做错了什么?

你有Content-Length: 0 ,所以问题在客户端。 您的Express代码看起来不错。

我遇到了很多问题,这帮助我解决了这个问题。

 var bodyParser = require('body-parser'); 

var app = express();

 // parse application/x-www-form-urlencoded **app.use(bodyParser.json());** app.use(bodyParser.urlencoded({ extended: false }));