forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (52 loc) · 1.7 KB
/
app.js
File metadata and controls
59 lines (52 loc) · 1.7 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// node modules
const express = require('express'),
app = express(),
util = require('./bin/util'),
fs = require('fs'),
axios = require('axios'),
covid19 = require('./lib/cli');
// set port
const port = process.env.port || 7070;
// package.json info
const pkg = JSON.parse(fs.readFileSync('package.json'));
// api base url
const apiBaseURL = "https://corona.lmao.ninja";
// global route for covid19 tracker
app.get('/', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
api = await axios.get(`${apiBaseURL}/all`),
data = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.covid19globaltracker(
data.cases, data.deaths,
data.recovered, data.updated
));
return null;
}
return next();
});
// by country route for covid19 tracker
app.get('/:country', async (req, res, next) => {
const userAgent = req.headers['user-agent'],
countryData = req.params.country,
api = await axios.get(`${apiBaseURL}/countries/${countryData}`),
all = await axios.get(`${apiBaseURL}/all`),
u = all.data,
d = api.data;
if (util.isCommandline(userAgent)) {
await res.send(covid19.covid19countrytracker(
d.country, d.cases, d.todayCases,
d.deaths, d.todayDeaths, d.recovered,
d.active, d.critical, d.casesPerOneMillion,
u.updated
));
return null;
}
return next();
});
app.get('*', (req, res) => res.send(`
Sorry, CLI version is only available at the moment...
\n
Try curl https://covid19tracker.xyz
\n`));
app.listen(port, () => console.log(`COVID-19 Tracker v${pkg.version} is listening on port ${port}!`));