How do you download a json file within an ExpressJS app using node-fetch
function getData(url) {
fetchSettings = {method: "Get"};
fetch(url, fetchSettings)
.then(res => res.json())
.then((json) => {
console.log(json); // <---- This shows the correct json object in the terminal
return json;
})
.catch(function (err) {
console.log(err);
});
}
app.get("/", (req, res) => {
url = "API JSON URL"
json = getData(url);
// Need to make multiple getData to different URLs
console.log(json); // <--- Returns "undefined"
});
I've also tried with await, but I believe I am misusing it
async function getData(url){
return await fetch(url, fetchSettings)
.then(res => res.json())
}
app.get("/", (req, res) => {
url = "API JSON URL"
json = getData(url);
// Need to make multiple getData to different URLs
console.log(json); // <--- Returns "Promise { <pending> }"
});
I need to make multiple getData() calls within the app.get() function in order to build an output json to serve to the webpage. So writing the rest of the code within the then() method within the fetch function is not an option.