I'm just getting back into Node.js after not looking at it for about 2 years. I'm wondering what the best way to handle the callbacks / "flow" of this program. Currently it rewrites the same file over and over. The filename doesn't stay synced to the data being downloaded. What is the best way to handle this?
It writes the last date as the filename - "2017-11-10.json". Specifically what mechanism do I use to get the correct d variable passed into the https.get. It doesn't allow extra variables to be passed. So what is the proper mechanism or scoping to achieve separate files for each response.
function dstr(date) {
let prep = (date.getMonth() + 1) + "/" + (date.getDay() + 1) + "/" + date.getFullYear();
let r = encodeURIComponent(prep);
return r;
}
var https = require('https');
var fs = require('fs');
let d = new Date("2017-11-15");
var url = "https://somewebsite.com/weather_stats/_Search?date={date}&last24=false";
let dates = [];
for (var i = 0; i < 5; i++) {
d.setDate(d.getDate() - 1);
dates.push(d);
}
dates.forEach( (x) => {
const u = url.replace("{date}", dstr(x));
https.get(u, (resp) => {
const filename = 'C:\\data\\' + x.toISOString().split('T')[0] + '.json'
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
console.log(JSON.parse(data).length);
fs.writeFile(filename, data, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
});