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;
});
}
}