简体   繁体   中英

GET request from server controller using MEAN stack

I'm using MEAN stack with MeanJs. The thing is, I have a task that requires calling a GET request from the server side (Expressjs) to another server (with a different domain name).

The code in the client side (AngularJs) calls:

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

And the corresponding server controller function is:

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

Obviously, the code below doesn't work. I'm unsure about how to make a GET request from that list function. Am I supposed to use ExpressJs or pure NodeJs for this? And how to get the correct library loaded?

Use the request module of nodejs : https://github.com/mikeal/request for sending the http request.

var request =  require("request");

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

Hope this helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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