Open gunnx opened 2 years ago
this one works for me:
koa: 2.13.4 @koa/router: 12.0.0
// ... a bunch of router.get()s;
// catch all for GET
router.get('/(.*)', (ctx) => {
ctx.status = 404;
ctx.body = 'Error 404 Not Found';
});
// catch all for all other verbs
router.all('/(.*)', (ctx) => {
ctx.status = 405;
ctx.body = 'Error 405 Method Not Allowed';
});
app.use(router.routes()).use(router.allowedMethods());
app.listen(3000);
hth
Koa version: 2.13.4 @koa/router version: 11.0.1
Test 1: Request [GET] http://localhost:3000/ // outputs 'Hello' (200) ✅ Test 2: Request [GET] http://localhost:3000/foo // outputs 'not found' (404) ✅ Test 3: Request [POST] http://localhost:3000/ // outputs 'not found' (405) expected Method not allowed ❌
If I try to log out the ctx.status in the final middleware for Test 3 it always show (404) - I never see the 405 from allowedMethods