Skip to content

Commit 0bf6e29

Browse files
committed
fix(server): fix POST request forward to routes defined in after option
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 0bf6e29

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

lib/Server.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,13 +390,25 @@ 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+
if (req.method !== 'GET') {
395+
return next();
396+
}
397+
398+
serveIndex(item)(req, res, next);
399+
});
394400
});
395401
} else if (
396402
typeof contentBase !== 'number' &&
397403
!isAbsoluteUrl(String(contentBase))
398404
) {
399-
this.app.use(contentBasePublicPath, serveIndex(contentBase));
405+
this.app.use(contentBasePublicPath, (req, res, next) => {
406+
if (req.method !== 'GET') {
407+
return next();
408+
}
409+
410+
serveIndex(contentBase)(req, res, next);
411+
});
400412
}
401413
}
402414

test/server/after-option.test.js

Lines changed: 14 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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)