【发布时间】:2016-11-27 23:53:10
【问题描述】:
我将 Angular 与 Node 一起使用,并且会从 GET 请求向客户端控制器发送响应。
当我使用本机 Node 而不是 Express.js 时,我用来发送响应的代码行似乎不起作用
这是我的角度控制器:
app.controller('testCtrl', function($scope, $http) {
$http.get('/test').then(function(response){
$scope = response.data;
});
});
这基本上被路由到以下服务器脚本:
http.createServer(function onRequest(req, res){
if(req.method==='GET'){
var scope = 'this is the test scope';
res.json(scope); // TypeError: res.json is not a function
}
}).listen(port);
res.json() 会抛出错误,除非我使用快速服务器
var app = express();
app.all('/*', function(req, res) {
if(req.method==='GET'){
var scope = 'this is the test scope';
res.json(scope); // OK!
}
}).listen(port);
我很确定 json() 函数随 node.js 一起提供。使用本机 Node 服务器时如何将响应发送回 Angular?
【问题讨论】:
标签: javascript angularjs json node.js