-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
44 lines (37 loc) · 1.01 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// imports
const express = require("express"),
app = express(),
fetch = require("node-fetch");
app.use(express.static("public"));
// https://expressjs.com/en/starter/basic-routing.html
app.get("/", (req, res) => {
res.sendFile(`${__dirname}/views/index.html`);
});
app.get("/t", (req, res) => {
res.sendFile(`${__dirname}/views/t.html`);
});
let world = [];
const update = () => {
const data = [];
fetch("https://pomber.github.io/covid19/timeseries.json")
.then(res => res.json())
.then(json => {
for (const country of Object.keys(json).sort()) {
data.push({ country, data: json[country][json[country].length - 1] });
}
world = data;
});
};
update();
{
const mins = 5;
setInterval(update, 60 * 1000 * mins);
}
// send the default array of dreams to the webpage
app.get("/data", (req, res) => {
res.json(world);
});
// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});