-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
44 lines (35 loc) · 983 Bytes
/
index.js
File metadata and controls
44 lines (35 loc) · 983 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var path = require('path');
var fs = require('mz/fs');
var send = require('koa-send');
var util = require('./lib/util');
var httpRxg = /^http/;
function respond(rule) {
return function *respond(next) {
for(var i = 0, len = rule.length; i < len; i++) {
var pattern = rule[i].pattern;
var responder = rule[i].responder;
if (typeof pattern == 'string') {
pattern = new RegExp(pattern);
}
if (!pattern.test(this.url)) {
continue;
}
responder = util.fixResponder(this.url, pattern, responder);
if (typeof responder == 'string') {
// 线上资源
if (httpRxg.test(responder)) {
this.url = responder;
// 本地资源
} else {
var stat = yield fs.stat(responder);
if (stat.isFile()) {
if (yield send(this, responder)) return;
}
}
}
break;
};
yield next;
};
}
module.exports = respond;