Search code examples
apiexpressmeanjscontrollers

How to call and HTTP route from express controller?


Hi i need some help for this issue, I need to run:

 exports.someFunction = function () {
   // i need call a route like this.
   app.route('api/getdata');
};

I need call some route in express function. How can I do it?


Solution

  • I'm assuming that /api/getdata returns some sort of data (in a format like JSON) you need to use. In that case, it is pretty simple, just use Node's excellent unirest library. I find Node's http a little advanced for this case. Assuming that this is a GET request, it would look something along the lines of:

    unirest.post('http://example.com/api/getdata').end(function (response) {
      console.log(response.body);
    });
    

    Of course, you can use Node's http.get if you wanted or any other HTTP library you like.