-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
30 lines (23 loc) · 828 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
"use strict"
const send = require("koa-send");
module.exports = function serve(path, root) {
// remove / begin
path = path.replace(/^\/+/, "");
return function(ctx, next) {
if(ctx.method == "HEAD" || ctx.method == "GET") {
let req_path_array = ctx.path.slice(1).split("/");
// match path
if(path.length == 0 || path == req_path_array[0]) {
// if not serve the root
// then remove the filtered folder from path
if(path.length != 0) {
req_path_array = req_path_array.slice(1);
}
return send(ctx, req_path_array.join("/") || "/", {root: root}).then(() => {
return next();
});
}
}
return next();
}
};