diff --git a/lib/Server.js b/lib/Server.js index 4edba202de..dff653aceb 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -390,13 +390,27 @@ class Server { if (Array.isArray(contentBase)) { contentBase.forEach((item) => { - this.app.use(contentBasePublicPath, serveIndex(item)); + this.app.use(contentBasePublicPath, (req, res, next) => { + // serve-index doesn't fallthrough non-get/head request to next middleware + if (req.method !== 'GET' && req.method !== 'HEAD') { + return next(); + } + + serveIndex(item)(req, res, next); + }); }); } else if ( typeof contentBase !== 'number' && !isAbsoluteUrl(String(contentBase)) ) { - this.app.use(contentBasePublicPath, serveIndex(contentBase)); + this.app.use(contentBasePublicPath, (req, res, next) => { + // serve-index doesn't fallthrough non-get/head request to next middleware + if (req.method !== 'GET' && req.method !== 'HEAD') { + return next(); + } + + serveIndex(contentBase)(req, res, next); + }); } } diff --git a/test/server/after-option.test.js b/test/server/after-option.test.js index b9d57616d0..9a0b07ea5e 100644 --- a/test/server/after-option.test.js +++ b/test/server/after-option.test.js @@ -29,6 +29,10 @@ describe('after option', () => { appArg.get('/after/some/path', (_, response) => { response.send('after'); }); + + appArg.post('/after/some/path', (_, response) => { + response.send('after POST'); + }); }, port, }, @@ -48,4 +52,14 @@ describe('after option', () => { expect(response.text).toBe('after'); }); }); + + it('should handle POST requests to after route', () => { + return req + .post('/after/some/path') + .expect('Content-Type', 'text/html; charset=utf-8') + .expect(200) + .then((response) => { + expect(response.text).toBe('after POST'); + }); + }); }); diff --git a/test/server/contentBasePublicPath-option.test.js b/test/server/contentBasePublicPath-option.test.js index b9eb5c3fa6..752002fcde 100644 --- a/test/server/contentBasePublicPath-option.test.js +++ b/test/server/contentBasePublicPath-option.test.js @@ -277,19 +277,19 @@ describe('contentBasePublicPath option', () => { }); it('POST request', (done) => { - req.post(`${contentBasePublicPath}/`).expect(405, done); + req.post(`${contentBasePublicPath}/`).expect(404, done); }); it('PUT request', (done) => { - req.put(`${contentBasePublicPath}/`).expect(405, done); + req.put(`${contentBasePublicPath}/`).expect(404, done); }); it('DELETE request', (done) => { - req.delete(`${contentBasePublicPath}/`).expect(405, done); + req.delete(`${contentBasePublicPath}/`).expect(404, done); }); it('PATCH request', (done) => { - req.patch(`${contentBasePublicPath}/`).expect(405, done); + req.patch(`${contentBasePublicPath}/`).expect(404, done); }); }); });