【问题标题】:GET request from server controller using MEAN stack使用 MEAN 堆栈从服务器控制器获取请求
【发布时间】:2014-07-08 05:56:46
【问题描述】:

我将 MEAN 堆栈与 MeanJs 一起使用。问题是,我有一个任务需要从服务器端(Expressjs)调用 GET 请求到另一台服务器(使用不同的域名)。

客户端(AngularJs)中的代码调用:

$scope.getWorkflow = function() {
    $http.get('/ezee', $scope.credentials).success(function(response) {
            console.log(response.message);
        }).error(function(response) {
            console.log('error');
        });
};

而对应的服务器控制器函数为:

exports.list = function(req, res) {
   req.get('http://ezslave.io', function(q, r){
       res.json({message: r.message}); // just to test
   });
};

显然,下面的代码不起作用。我不确定如何从 list 函数发出 GET 请求。我应该为此使用 ExpressJs 还是纯 NodeJs?以及如何加载正确的库?

【问题讨论】:

标签: node.js angularjs express mean-stack


【解决方案1】:

使用nodejs的请求模块:https://github.com/mikeal/request 用于发送 http 请求。

var request =  require("request");

exports.list = function(req, res) {
      request("http://ezslave.io",function(err,response,body){
           res.send(response);
    });          
   };

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多