Skip to content

Commit

Permalink
add support serve root folder fixed #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Secbone committed Jul 27, 2016
1 parent 17d78a3 commit 1b4d843
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ module.exports = function serve(path, root) {
return function(ctx, next) {
if(ctx.method == "HEAD" || ctx.method == "GET") {

let pathArr = ctx.path.slice(1).split("/");
let req_path_array = ctx.path.slice(1).split("/");

// match path
if(path == pathArr[0]) {
return send(ctx, pathArr.slice(1).join("/"), {root: root}).then(() => {
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();
});
}
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ describe("Koa-static2", () => {
.expect(200, "text content\n", done)
});

it("should work when serve the root path", done => {
const app = new Koa();
app.use(serve("/", __dirname));

request(app.listen())
.get("/some.txt")
.expect(200, "text content\n", done)
});
});

0 comments on commit 1b4d843

Please sign in to comment.