【发布时间】:2014-11-19 15:13:28
【问题描述】:
我遇到了无法诊断的问题。
在服务器上,我有一个使用 Express.js 的简单 URL 处理程序:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var multer = require('multer');
app.configure(function() {
app.use(app.router);
app.use(bodyParser.json()); // see: http://expressjs.com/api.html#req.body
app.use(bodyParser.urlencoded({
extended: true
}));
});
app.post('/submit', function (req, res) {
console.log(req.body);
});
在客户端,有一个由 Angular 控制器处理的表单:
$scope.submit = function () {
// $http.post('/submit', $scope.data); // POST request to send data to the server
$http({
method: 'POST',
url: '/submit',
data: $scope.data
});
console.log('POST /submit ' + JSON.stringify($scope.data));
};
在浏览器的控制台中一切正常:$scope.data 有效;正如预期的那样,Node.js 也以console.log 响应,但写入undefined,这意味着request.body 未定义。
我做错了什么?我该如何解决?
【问题讨论】:
-
浏览器控制台中网络选项卡中的请求是否有正文?
-
您使用的是什么版本的 express?文档说:
All routing methods will be added in the order in which they appear. You should not do app.use(app.router). This eliminates the most common issue with Express.github.com/strongloop/express/wiki/New-features-in-4.x -
我使用 Express 3.2.4,我完全确定
app.router超出了这个问题的范围。
标签: angularjs node.js express httprequest