2

I think my issue is probably due to not having a callback / promise for use with a async request call.

However, I still have not fully wrapped my head around async callbacks with node.js.

I am hoping someone can explain how I can get my Express route to call my API that is included in a separate file called myapi.js which contains a function that is exported for usage by the main.js which is running Express.

I want the returned response from the exported function call to be sent to the browser via res.send.

Any help is much appreciated!

[main.js - Express App]

var myapi = require('./myapi');

// all of my express stuff goes here and eventually i setup my route below

app.get('/orders/complete/:date', function (req, res) {
      res.send(myapi.getCompletedOrders(req.params.date));
});

[myapi.js]

var request = require('request');

module.exports = {

  getCompletedOrders: function(date){
    request('https://some.api.getorders.json?' + '&start=' + date, function (error, response, body) {

          //Check for error
          if(error){
              return console.log('Error:', error);
          }
          //Check for success status code
          if(response.statusCode !== 200){
              return console.log('Invalid Status Code Returned:', response.statusCode);
          }
          var allCompletedOrders = JSON.parse(body);
          return allCompletedOrders;
      });
  }
}
jremi
  • 2,879
  • 3
  • 26
  • 33

2 Answers2

3

As you guessed you couldn't simply return a value from an asynchronous function call. you should use callbacks or promises:

The callback way:

myapi.js

var request = require('request');

module.exports = {

  getCompletedOrders: function(date, callback) {

    request('https://some.api.getorders.json?' + '&start=' + date, function(error, response, body) {

      //Check for error
      if (error) {
        callback(error);
      }
      //Check for success status code
      if (response.statusCode !== 200) {
        callback(new Error('Invalid Status Code Returned:' + response.statusCode));
      }
      var allCompletedOrders = JSON.parse(body);
      callback(null, allCompletedOrders);
    });
  }

}

main.js

var myapi = require('./myapi');

// all of my express stuff goes here and eventually i setup my route below

app.get('/orders/complete/:date', function(req, res, next) {
  myapi.getCompletedOrders(req.params.date, function(error, orders) {
    if ( error ) {
      next(error);
    }
    res.send(orders);
  });
});

Read More: How do I return the response from an asynchronous call? And implement promise way if you prefer.

Community
  • 1
  • 1
dNitro
  • 5,145
  • 2
  • 20
  • 45
  • 1
    This is extremely helpful for me to better understand what I was missing. Thank you for your reply. – jremi Feb 14 '17 at 22:24
1

[main.js - Express App]

var myapi = require('./myapi');

// all of my express stuff goes here and eventually i setup my route below

app.get('/orders/complete/:date', function (req, res) {
  myapi.getCompletedOrders(req.params.date).pipe(res);
});

[myapi.js]

var request = require('request');

module.exports = {

  getCompletedOrders: function(date){
    //  HTTPS GET - Order List Unfulfilled - Request to TopHatters's website.
    return request('https://some.api.getorders.json?' + '&start=' + date);
  }
}
idbehold
  • 16,833
  • 5
  • 47
  • 74