Skip to content

Commit 1d515fb

Browse files
committed
add vercel cache
1 parent c3b5fba commit 1d515fb

File tree

2 files changed

+3944
-58
lines changed

2 files changed

+3944
-58
lines changed

app.js

Lines changed: 98 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const { lookupCountry, htmlTemplate, footer } = require('./lib/helpers');
1313
const { getLiveUpdates } = require('./lib/reddit.js');
1414
const { getWorldoMetersTable } = require('./lib/worldoMeters.js');
1515
const { getUsaStats } = require('./lib/country/us.js');
16-
const { helpContent, countryNotFound, stateCountryNotFound } = require('./lib/constants');
16+
const {
17+
helpContent,
18+
countryNotFound,
19+
stateCountryNotFound,
20+
} = require('./lib/constants');
1721

1822
const app = express();
1923
const port = process.env.PORT || 3001;
@@ -24,7 +28,7 @@ function errorHandler(error, req, res) {
2428
const body = `
2529
I am sorry. Something went wrong. Please report it\n
2630
${error.message}
27-
${footer(new Date)}
31+
${footer(new Date())}
2832
`;
2933
if (req.isCurl) {
3034
return body;
@@ -34,27 +38,35 @@ function errorHandler(error, req, res) {
3438

3539
app.set('json escape', true);
3640

37-
app.use(helmet({
38-
dnsPrefetchControl: false,
39-
frameguard: {
40-
action: 'deny'
41-
}
42-
}));
43-
44-
app.use(helmet.hsts({
45-
force: true,
46-
includeSubDomains: true,
47-
maxAge: 63072000, // 2 years
48-
preload: true
49-
}));
41+
app.use(
42+
helmet({
43+
dnsPrefetchControl: false,
44+
frameguard: {
45+
action: 'deny',
46+
},
47+
}),
48+
);
49+
50+
app.use(
51+
helmet.hsts({
52+
force: true,
53+
includeSubDomains: true,
54+
maxAge: 63072000, // 2 years
55+
preload: true,
56+
}),
57+
);
5058

5159
app.use(helmet.referrerPolicy({ policy: 'strict-origin-when-cross-origin' }));
5260

53-
app.use(morgan(':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms'));
61+
app.use(
62+
morgan(
63+
':remote-addr :remote-user :method :url :status :res[content-length] - :response-time ms',
64+
),
65+
);
5466
app.use('/favicon.ico', express.static('./favicon.ico'));
5567

5668
app.use((req, res, next) => {
57-
res.setHeader('Cache-Control', 'no-cache');
69+
res.setHeader('Cache-Control', 's-maxage=600');
5870
req.isCurl = IS_CURL_RE.test(req.headers['user-agent']);
5971
next();
6072
});
@@ -69,55 +81,62 @@ app.get('/', (req, res) => {
6981

7082
if (source === 1) {
7183
if (format.toLowerCase() === 'json') {
72-
return getJSONData().then(result => {
73-
return res.json(result);
74-
}).catch(error => errorHandler(error, req, res));
84+
return getJSONData()
85+
.then((result) => {
86+
return res.json(result);
87+
})
88+
.catch((error) => errorHandler(error, req, res));
7589
}
7690

7791
return getCompleteTable({ isCurl, emojis, minimal, top })
78-
.then(result => {
92+
.then((result) => {
7993
return res.send(result);
80-
}).catch(error => errorHandler(error, req, res));
94+
})
95+
.catch((error) => errorHandler(error, req, res));
8196
}
8297

83-
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format})
84-
.then(result => {
98+
return getWorldoMetersTable({ isCurl, emojis, minimal, top, format })
99+
.then((result) => {
85100
return res.send(result);
86-
}).catch(error => errorHandler(error, req, res));
87-
101+
})
102+
.catch((error) => errorHandler(error, req, res));
88103
});
89104

90105
app.get('/updates', (req, res) => {
91106
const isCurl = req.isCurl;
92107
const format = req.query.format ? req.query.format : '';
93108

94109
if (format.toLowerCase() === 'json') {
95-
return getLiveUpdates({ json: true, isCurl }).then(result => {
96-
return res.json(result);
97-
}).catch(error => errorHandler(error, req, res));
110+
return getLiveUpdates({ json: true, isCurl })
111+
.then((result) => {
112+
return res.json(result);
113+
})
114+
.catch((error) => errorHandler(error, req, res));
98115
}
99116

100-
return getLiveUpdates({ json: false, isCurl }).then(result => {
101-
return res.send(result);
102-
}).catch(error => errorHandler(error, req, res));
117+
return getLiveUpdates({ json: false, isCurl })
118+
.then((result) => {
119+
return res.send(result);
120+
})
121+
.catch((error) => errorHandler(error, req, res));
103122
});
104123

105124
app.get(['/:country/graph', '/graph'], (req, res) => {
106125
const { country } = req.params;
107126
const isCurl = req.isCurl;
108127
if (!country) {
109128
return getGraph({ isCurl })
110-
.then(result => res.send(result))
111-
.catch(error => errorHandler(error, req, res));
129+
.then((result) => res.send(result))
130+
.catch((error) => errorHandler(error, req, res));
112131
}
113132
const lookupObj = lookupCountry(country);
114133

115134
if (!lookupObj) {
116135
return res.status(404).send(countryNotFound(isCurl));
117136
}
118-
return getGraph({countryCode: lookupObj.iso2, isCurl })
119-
.then(result => res.send(result))
120-
.catch(error => errorHandler(error, req, res));
137+
return getGraph({ countryCode: lookupObj.iso2, isCurl })
138+
.then((result) => res.send(result))
139+
.catch((error) => errorHandler(error, req, res));
121140
});
122141

123142
app.get('/help', (req, res) => {
@@ -141,14 +160,14 @@ app.get('/states/:country', (req, res) => {
141160
return res.status(404).send(stateCountryNotFound(isCurl));
142161
}
143162
if (lookupObj.iso2 === 'US') {
144-
return getUsaStats({ isCurl, minimal, top, format})
145-
.then(result => {
163+
return getUsaStats({ isCurl, minimal, top, format })
164+
.then((result) => {
146165
return res.send(result);
147-
}).catch(error => errorHandler(error, req, res));
166+
})
167+
.catch((error) => errorHandler(error, req, res));
148168
}
149169
});
150170

151-
152171
app.get('/:country', (req, res) => {
153172
const { country } = req.params;
154173
const isCurl = req.isCurl;
@@ -159,15 +178,29 @@ app.get('/:country', (req, res) => {
159178

160179
if (!country || country.toUpperCase() === 'ALL' || country.includes(',')) {
161180
if (format.toLowerCase() === 'json') {
162-
return getWorldoMetersTable({ countryCode: country, isCurl, emojis, minimal, format }).then(result => {
163-
return res.json(result);
164-
}).catch(error => errorHandler(error, req, res));
181+
return getWorldoMetersTable({
182+
countryCode: country,
183+
isCurl,
184+
emojis,
185+
minimal,
186+
format,
187+
})
188+
.then((result) => {
189+
return res.json(result);
190+
})
191+
.catch((error) => errorHandler(error, req, res));
165192
}
166193

167-
return getWorldoMetersTable({ countryCode: country, isCurl, emojis, minimal })
168-
.then(result => {
194+
return getWorldoMetersTable({
195+
countryCode: country,
196+
isCurl,
197+
emojis,
198+
minimal,
199+
})
200+
.then((result) => {
169201
return res.send(result);
170-
}).catch(error => errorHandler(error, req, res));
202+
})
203+
.catch((error) => errorHandler(error, req, res));
171204
}
172205
if (source === 1) {
173206
const lookupObj = lookupCountry(country);
@@ -178,14 +211,17 @@ app.get('/:country', (req, res) => {
178211
const { iso2 } = lookupObj;
179212

180213
if (format.toLowerCase() === 'json') {
181-
return getJSONDataForCountry(iso2).then(result => {
182-
return res.json(result);
183-
}).catch(error => errorHandler(error, req, res));
214+
return getJSONDataForCountry(iso2)
215+
.then((result) => {
216+
return res.json(result);
217+
})
218+
.catch((error) => errorHandler(error, req, res));
184219
}
185220
return getCountryTable({ countryCode: iso2, isCurl, emojis, minimal })
186-
.then(result => {
221+
.then((result) => {
187222
return res.send(result);
188-
}).catch(error => errorHandler(error, req, res));
223+
})
224+
.catch((error) => errorHandler(error, req, res));
189225
}
190226

191227
const lookupObj = lookupCountry(country);
@@ -196,10 +232,17 @@ app.get('/:country', (req, res) => {
196232

197233
const { iso2 } = lookupObj;
198234

199-
return getWorldoMetersTable({ countryCode: iso2, isCurl, emojis, minimal, format })
200-
.then(result => {
235+
return getWorldoMetersTable({
236+
countryCode: iso2,
237+
isCurl,
238+
emojis,
239+
minimal,
240+
format,
241+
})
242+
.then((result) => {
201243
return res.send(result);
202-
}).catch(error => errorHandler(error, req, res));
244+
})
245+
.catch((error) => errorHandler(error, req, res));
203246
});
204247

205248
app.listen(port, () => console.log(`Running on ${port}`));

0 commit comments

Comments
 (0)