I have now replaced the body of the tutorial function with a simple Http request using the built in https module included in NodeJS but it does not seem to be connecting to my API.
For the purpose of this question I have included the code snippet with an example http request that follows the same strategy I have been trying to implement with my own API.
Running the serverless function below I do not seem to be hitting the API code and the functionResponse is not updated with the result of the call.
exports.main = (context, sendResponse) => {
// your code called when the function is executed
var functionResponse = "Congrats! You've just deployed a Serverless Function."
const https = require('https');
let data = '';
https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => {
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).explanation);
functionResponse = JSON.parse(data).explanation;
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
sendResponse({body: functionResponse, statusCode: 200});
};