forked from sagarkarira/coronavirus-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
110 lines (90 loc) · 3.3 KB
/
app.js
File metadata and controls
110 lines (90 loc) · 3.3 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express');
const morgan = require('morgan');
const { getCountryTable, getJSONData, getJSONDataForCountry } = require('./lib/byCountry');
const { getCompleteTable } = require('./lib/corona');
const { lookupCountry } = require('./lib/helpers');
const { getLiveUpdates } = require('./lib/reddit.js');
const app = express();
const port = process.env.PORT || 3001;
const IS_CURL_RE = /\bcurl\b/im;
function errorHandler(error, res) {
console.error(error);
return res.send(`
I am sorry. Something went wrong. Please report it\n
${error.message}
`);
}
app.set('json escape', true);
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache');
next();
});
app.get('/', (req, res) => {
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';
const top = req.query.top ? Number(req.query.top) : 1000;
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getCompleteTable({ isCurl, emojis, minimal, top })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
app.get('/updates', (req, res) => {
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
const format = req.query.format ? req.query.format : '';
if (format.toLowerCase() === 'json') {
return getLiveUpdates({ json: true, isCurl }).then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getLiveUpdates({ json: false, isCurl }).then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
app.get('/:country', (req, res) => {
const { country } = req.params;
const isCurl = IS_CURL_RE.test(req.headers['user-agent']);
const format = req.query.format ? req.query.format : '';
const minimal = req.query.minimal === 'true';
const emojis = req.query.emojis === 'true';
if (!country || country.toUpperCase() === 'ALL') {
if (format.toLowerCase() === 'json') {
return getJSONData().then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getCompleteTable({ isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
}
const lookupObj = lookupCountry(country);
if (!lookupObj) {
return res.send(`
Country not found.
Try the full country name or country code.
Example:
- /UK: for United Kingdom
- /US: for United States of America.
- /Italy: for Italy.
`);
}
const { iso2 } = lookupObj;
if (format.toLowerCase() === 'json') {
return getJSONDataForCountry(iso2).then(result => {
return res.json(result);
}).catch(error => errorHandler(error, res));
}
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
.then(result => {
return res.send(result);
}).catch(error => errorHandler(error, res));
});
app.listen(port, () => console.log(`Running on ${port}`));