forked from javascript-tutorial/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconditional.js
More file actions
executable file
·27 lines (20 loc) · 790 Bytes
/
Copy pathconditional.js
File metadata and controls
executable file
·27 lines (20 loc) · 790 Bytes
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
let conditional = require('koa-conditional-get');
let etag = require('koa-etag');
exports.init = function(app) {
// use it upstream from etag so
// that they are present
// conditional triggers AFTER other middleware and returns 304/empty body if etag/modified matches
app.use(conditional());
// add etags AFTER every request (even POST), using file/body content and crc32
app.use(etag());
// set expires to ctx.expires
app.use(async function (ctx, next) {
await next();
if (!ctx.expires) return;
if (process.env.NODE_ENV === 'development') {
// override any value with 2 secs, we don't need long expires to block our changes in dev
ctx.expires = 2;
}
ctx.set('Expires', new Date(Date.now() + ctx.expires*1e3).toUTCString());
});
};