-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
45 lines (34 loc) · 931 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
36
37
38
39
40
41
42
43
44
45
/**
* Module dependencies.
*/
var resolve = require('path').resolve;
var fs = require('fs');
/**
* Deliver robots.txt
*
* @param {String} path
* @param {Object} [options]
* @return {Function}
* @api public
*/
module.exports = function (path, options){
var file;
if (path) path = resolve(path);
options = options || {};
var maxAge = options.maxAge == null
? 86400000
: Math.min(Math.max(0, options.maxAge), 31556926000);
return function *roboxtxt(next){
if ('/robots.txt' != this.path) return yield next;
if (!path) return;
if ('GET' !== this.method && 'HEAD' !== this.method) {
this.status = 'OPTIONS' == this.method ? 200 : 405;
this.set('Allow', 'GET, HEAD, OPTIONS');
return;
}
if (!file) file = fs.readFileSync(path);
this.set('Cache-Control', 'public, max-age=' + (maxAge / 1000 | 0));
this.type = 'text/plain';
this.body = file;
};
};