Skip to content

Commit c34e15f

Browse files
committed
added cache control header for zeit cdn smart caching
1 parent 29d1e68 commit c34e15f

File tree

4 files changed

+971
-1
lines changed

4 files changed

+971
-1
lines changed

app.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const express = require('express');
22
const app = express();
33
const lookup = require('country-code-lookup');
4+
const morgan = require('morgan');
45

56
const port = process.env.PORT || 3001;
67

@@ -12,16 +13,20 @@ function errorHandler(error, res) {
1213
return res.send('I am sorry. Something went wrong. Please report it');
1314
}
1415

16+
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
17+
1518
app.get('/', (req, res) => {
1619
const format = req.query.format ? req.query.format : '';
1720

1821
if (format.toLowerCase() === 'json') {
1922
return getJSONData().then(result => {
23+
res.setHeader('Cache-Control', 's-maxage=86400');
2024
return res.json(result);
2125
}).catch(error => errorHandler(error, res));
2226
}
2327

2428
return getCompleteTable().then(result => {
29+
res.setHeader('Cache-Control', 's-maxage=900');
2530
return res.send(result);
2631
}).catch(error => errorHandler(error, res));
2732
});
@@ -34,11 +39,13 @@ app.get('/:country', (req, res) => {
3439
if (!country || country === 'all') {
3540
if (format.toLowerCase() === 'json') {
3641
return getJSONData().then(result => {
42+
res.setHeader('Cache-Control', 's-maxage=900');
3743
return res.json(result);
3844
}).catch(error => errorHandler(error, res));
3945
}
4046

4147
return getCompleteTable().then(result => {
48+
res.setHeader('Cache-Control', 's-maxage=900');
4249
return res.send(result);
4350
}).catch(error => errorHandler(error, res));
4451
}
@@ -64,11 +71,13 @@ app.get('/:country', (req, res) => {
6471

6572
if (format.toLowerCase() === 'json') {
6673
return getJSONDataForCountry(iso2).then(result => {
74+
res.setHeader('Cache-Control', 's-maxage=900');
6775
return res.json(result);
6876
}).catch(error => errorHandler(error, res));
6977
}
7078

7179
return getCountryTable(iso2).then(result => {
80+
res.setHeader('Cache-Control', 's-maxage=900');
7281
return res.send(result);
7382
}).catch(error => errorHandler(error, res));
7483
});

lib/helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ e.getTotalStats = (countryData) => {
147147

148148
e.footer = `
149149
150-
Stay safe everyone.
150+
Stay safe. Stay inside.
151151
152152
Code: https://github.com/sagarkarira/coronavirus-tracker-cli/
153153
Twitter: http://twitter.com/ekrysis
154+
154155
`;

0 commit comments

Comments
 (0)