From 1328f78842026e17746d8dfc0c79928124bc9bf5 Mon Sep 17 00:00:00 2001 From: 3went2 Date: Tue, 4 Jul 2017 17:04:38 +0800 Subject: [PATCH] fix `index: false` bug if path is directory fix `index: false` bug if path is directory Revert "fix `index: false` bug if path is directory" This reverts commit b952196790ebe291bd17f1eb103708e26d73c41d. fix `index: false` bug if path is directory closes #104 --- index.js | 10 ++++++---- test/index.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 46ae4a9..e8110d7 100644 --- a/index.js +++ b/index.js @@ -37,16 +37,18 @@ function serve (root, opts) { if (!opts.defer) { return async function serve (ctx, next) { + let done = false + if (ctx.method === 'HEAD' || ctx.method === 'GET') { try { - await send(ctx, ctx.path, opts) + done = await send(ctx, ctx.path, opts) } catch (err) { - await next() } - return } - await next() + if (!done) { + await next() + } } } diff --git a/test/index.js b/test/index.js index b3c1347..e2ea964 100644 --- a/test/index.js +++ b/test/index.js @@ -132,6 +132,19 @@ describe('serve(root)', function () { .get('/world/') .expect(404, done) }) + + it('should pass to downstream if 404', function (done) { + const app = new Koa() + + app.use(serve('test/fixtures', { index: false })) + app.use(async (ctx) => { + ctx.body = 'oh no' + }) + + request(app.listen()) + .get('/world/') + .expect('oh no', done) + }) }) })