Skip to content

Commit 7a77b4a

Browse files
committed
Render monospaced table in HTML when not curl request
1 parent f9a0ec7 commit 7a77b4a

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

app.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function errorHandler(error, res) {
1818
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
1919

2020
app.get('/', (req, res) => {
21+
const isCurl = req.headers['user-agent'].match(/\bcurl\b/gmi) !== null
2122
const format = req.query.format ? req.query.format : '';
2223

2324
if (format.toLowerCase() === 'json') {
@@ -27,7 +28,7 @@ app.get('/', (req, res) => {
2728
}).catch(error => errorHandler(error, res));
2829
}
2930

30-
return getCompleteTable().then(result => {
31+
return getCompleteTable({isCurl}).then(result => {
3132
res.setHeader('Cache-Control', 's-maxage=900');
3233
return res.send(result);
3334
}).catch(error => errorHandler(error, res));

bin/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const { argv } = yargs
5050
const { emojis, country } = argv;
5151
(
5252
country === 'ALL'
53-
? getCompleteTable(emojis)
53+
? getCompleteTable({emojis})
5454
: getCountryTable(country, emojis)
5555
)
5656
.then(console.log)

lib/api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const NodeCache = require( "node-cache" );
1+
const NodeCache = require('node-cache');
22
const axios = require('axios');
33
const myCache = new NodeCache( { stdTTL: 100, checkperiod: 600 } );
44
const CORONA_ALL_KEY = 'coronaAll';

lib/corona.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function extraStats(dataArr) {
7979
);
8080
}
8181

82-
exports.getCompleteTable = async (emojis = false) => {
82+
exports.getCompleteTable = async ({isCurl, emojis = false}) => {
8383
const head = [
8484
'',
8585
'Country',
@@ -139,5 +139,26 @@ exports.getCompleteTable = async (emojis = false) => {
139139
table.push({ [rank++]: values })
140140
});
141141
const lastUpdated = countryData[0].lastUpdated;
142+
if (!isCurl) {
143+
const template = `<!DOCTYPE html>
144+
<html lang="en">
145+
<head>
146+
<meta charset="UTF-8">
147+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
148+
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
149+
<title>Coronavirus Tracker</title>
150+
<style>
151+
pre {
152+
font-family: 'Roboto Mono', monospace;
153+
white-space: pre;
154+
}
155+
</style>
156+
</head>
157+
<body>
158+
<pre>${table.toString() + footer(lastUpdated)}</pre>
159+
</body>
160+
</html>`
161+
return template;
162+
}
142163
return table.toString() + footer(lastUpdated);
143-
}
164+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"corona": "./bin/index.js"
88
},
99
"scripts": {
10-
"dev": "nodemon app.js",
10+
"dev": "nodemon --inspect app.js",
1111
"start": "node app.js",
1212
"test": "echo \"Error: no test specified\" && exit 1"
1313
},

0 commit comments

Comments
 (0)