繁体   English   中英

在Node.js中发送对Angular.js的回复

[英]Send a response to Angular.js in Node.js

我使用Angular with Node,并将从GET请求向客户端控制器发送响应。

当我使用本机节点而不是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()函数带有节点。 使用本机节点服务器时,如何将响应发送回角度?

res.json不在HTTP响应的API中。

标准方法是使用res.write()docs )然后使用res.end()docs )。 但在你的情况下,由于你没有发送大量数据,你可以使用快捷方式,只需使用带有data参数的res.end() :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM