Skip to content

Commit ebe8eca

Browse files
jvasseurhiroppy
authored andcommitted
fix(server): fallthrough non GET and HEAD request to routes… (#2374)
Fix a bug introduced in cee700d where the serveIndex feature where always replying instead of forwarding requests to the next middleware.
1 parent f4c8f94 commit ebe8eca

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

lib/Server.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,27 @@ class Server {
390390

391391
if (Array.isArray(contentBase)) {
392392
contentBase.forEach((item) => {
393-
this.app.use(contentBasePublicPath, serveIndex(item));
393+
this.app.use(contentBasePublicPath, (req, res, next) => {
394+
// serve-index doesn't fallthrough non-get/head request to next middleware
395+
if (req.method !== 'GET' && req.method !== 'HEAD') {
396+
return next();
397+
}
398+
399+
serveIndex(item)(req, res, next);
400+
});
394401
});
395402
} else if (
396403
typeof contentBase !== 'number' &&
397404
!isAbsoluteUrl(String(contentBase))
398405
) {
399-
this.app.use(contentBasePublicPath, serveIndex(contentBase));
406+
this.app.use(contentBasePublicPath, (req, res, next) => {
407+
// serve-index doesn't fallthrough non-get/head request to next middleware
408+
if (req.method !== 'GET' && req.method !== 'HEAD') {
409+
return next();
410+
}
411+
412+
serveIndex(contentBase)(req, res, next);
413+
});
400414
}
401415
}
402416

test/server/after-option.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ describe('after option', () => {
2929
appArg.get('/after/some/path', (_, response) => {
3030
response.send('after');
3131
});
32+
33+
appArg.post('/after/some/path', (_, response) => {
34+
response.send('after POST');
35+
});
3236
},
3337
port,
3438
},
@@ -48,4 +52,14 @@ describe('after option', () => {
4852
expect(response.text).toBe('after');
4953
});
5054
});
55+
56+
it('should handle POST requests to after route', () => {
57+
return req
58+
.post('/after/some/path')
59+
.expect('Content-Type', 'text/html; charset=utf-8')
60+
.expect(200)
61+
.then((response) => {
62+
expect(response.text).toBe('after POST');
63+
});
64+
});
5165
});

test/server/contentBasePublicPath-option.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,19 @@ describe('contentBasePublicPath option', () => {
277277
});
278278

279279
it('POST request', (done) => {
280-
req.post(`${contentBasePublicPath}/`).expect(405, done);
280+
req.post(`${contentBasePublicPath}/`).expect(404, done);
281281
});
282282

283283
it('PUT request', (done) => {
284-
req.put(`${contentBasePublicPath}/`).expect(405, done);
284+
req.put(`${contentBasePublicPath}/`).expect(404, done);
285285
});
286286

287287
it('DELETE request', (done) => {
288-
req.delete(`${contentBasePublicPath}/`).expect(405, done);
288+
req.delete(`${contentBasePublicPath}/`).expect(404, done);
289289
});
290290

291291
it('PATCH request', (done) => {
292-
req.patch(`${contentBasePublicPath}/`).expect(405, done);
292+
req.patch(`${contentBasePublicPath}/`).expect(404, done);
293293
});
294294
});
295295
});

0 commit comments

Comments
 (0)