Skip to content

Global middleware for 404 overwrites allowed methods response output #157

Open
@gunnx

Description

Koa version: 2.13.4
@koa/router version: 11.0.1

const Koa = require('koa');
const Router = require('@koa/router');

const app = new Koa();
const router = new Router();

router.get('/', async(ctx, next) => {
  ctx.status = 200;
  ctx.body = ' Hello!'
  await next();
})

app.use(router.routes());
app.use(router.allowedMethods());

// catch all to handle non-matched routes
app.use(ctx => {
  ctx.status = 404;
  ctx.body = 'not found'
});

app.listen(3000);

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions