-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 836 Bytes
/
index.js
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
/**
* create middleware
*
* @param {Object} option
* @config {RegExp} pattern
* @config {Number} duration
*/
module.exports = function (option) {
/**
* set expires to response headers
*
* @param {express.Request} req
* @param {express.Response} res
* @param {Function} next middleware iterator
*/
return function (req, res, next) {
var url = req.url
, now
;
option = option || {};
// default one day
option.duration = (option.duration == null) ? (1000 * 60 * 60 * 24) : +option.duration;
if (!option.pattern || !option.pattern.test(url)) {
return next();
}
now = (new Date(Date.now() + option.duration)).toUTCString();
res.setHeader('Expires', now);
res.setHeader('Cache-Control', "max-age=" + (option.duration / 1000));
next();
};
};