I have the following function that is called on every request.
async function checkForNewData() {
var now = moment();
var lastUpdateUnix = fs.readFileSync('.data/last-update.txt').toString();
var lastUpdate = moment.duration(lastUpdateUnix, 'seconds');
now.subtract(lastUpdate);
if (now.hour() >= 1) {
console.log("Last update is over 1 hour old. Getting new data.");
// Schedule
console.log("Getting new schedule.")
let res = await axios.get('https://splatoon2.ink/data/schedules.json', { headers: { "User-Agent": "Splatoon2.ink caching server at glitch.com/~splatoon2-ink-cache" } });
fs.writeFileSync('.data/rotations.json', JSON.stringify(res.data));
// Image
console.log("Getting new image.");
let resImage = await axios.get('https://splatoon2.ink/twitter-images/schedule.png', { headers: { "User-Agent": "Splatoon2.ink caching server at glitch.com/~splatoon2-ink-cache" }, responseType: 'stream' });
const path = Path.resolve(__dirname, '.data', 'image.png');
const writer = fs.createWriteStream(path);
resImage.data.pipe(writer);
fs.writeFileSync('.data/last-update.txt', moment().unix());
console.log("Data is now up to date.");
return;
}
}
On every Express route I have something similar to this.
app.get('/', function(request, response) {
console.log("Request for schedule.");
checkForNewData().then(function() {
console.log("Sending schedule.");
response.sendFile(__dirname + '/.data/rotations.json');
});
});
My goal is to run the function which will check for if the current data is outdated (using the moment library) and if it is, then it gets new data. But for some reason, when using axios to get the image part, it won't be written to the file system. I've tried using different approaches to saving it but everything I've tried won't work.
Here's my full file.
// server.js
// where your node app starts
// init project
const express = require('express');
const app = express();
const moment = require('moment');
const fs = require("fs");
const Path = require('path');
const axios = require("axios")
fs.writeFileSync('.data/last-update.txt', '0');
fs.writeFileSync('.data/rotations.json', '{}');
// we've started you off with Express,
// but feel free to use whatever libs or frameworks you'd like through `package.json`.
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
async function checkForNewData() {
var now = moment();
var lastUpdateUnix = fs.readFileSync('.data/last-update.txt').toString();
var lastUpdate = moment.duration(lastUpdateUnix, 'seconds');
now.subtract(lastUpdate);
if (now.hour() >= 1) {
console.log("Last update is over 1 hour old. Getting new data.");
// Schedule
console.log("Getting new schedule.")
let res = await axios.get('https://splatoon2.ink/data/schedules.json', { headers: { "User-Agent": "Splatoon2.ink caching server at glitch.com/~splatoon2-ink-cache" } });
fs.writeFileSync('.data/rotations.json', JSON.stringify(res.data));
// Image
console.log("Getting new image.");
let resImage = await axios.get('https://splatoon2.ink/twitter-images/schedule.png', { headers: { "User-Agent": "Splatoon2.ink caching server at glitch.com/~splatoon2-ink-cache" }, responseType: 'stream' });
const path = Path.resolve(__dirname, '.data', 'image.png');
const writer = fs.createWriteStream(path);
resImage.data.pipe(writer);
fs.writeFileSync('.data/last-update.txt', moment().unix());
console.log("Data is now up to date.");
return;
}
}
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
console.log("Request for schedule.");
checkForNewData().then(function() {
console.log("Sending schedule.");
response.sendFile(__dirname + '/.data/rotations.json');
});
});
app.get('/image', function(request, response) {
console.log("Request for image.");
checkForNewData().then(function() {
console.log("Sending image.");
response.type('png');
response.sendFile(__dirname + '/.data/image.png');
});
});
// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});