koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.
MIT License
861 stars 178 forks source link

[fix] When I use multiple routing instances and the request method is incorrect(Define post but use get), the error message is "undefined is not an object (evaluating 'route.methods.length'" #184

Closed zhaoyuqiqi closed 1 month ago

zhaoyuqiqi commented 1 month ago

Describe the bug

Node.js version: 20.11.1

OS version: MacBook Pro macOS 14.1.2

Description: When I use multiple routing instances and the request method is incorrect(Define post but use get), the error message is "undefined is not an object (evaluating 'route.methods.length'"

Actual behavior

throw an error

Expected behavior

No-throw error

Code to reproduce

import Koa from 'koa';
import KoaRouter from '@koa/router';

const app = new Koa();
const userRouter = new KoaRouter();
userRouter.prefix('/user');
userRouter.post('/login', (ctx, next) => {
  ctx.body = 'login success';
});
const teamRouter = new KoaRouter();
teamRouter.prefix('/team');
teamRouter.post('/test', (ctx, next) => {
  ctx.body = 'test success';
});

app.use(userRouter.routes());
app.use(
  userRouter.allowedMethods({
    throw: true,
    notImplemented: () => new Error('notImplemented'),
    methodNotAllowed: () => new Error('methodNotAllowed')
  })
);
app.use(teamRouter.routes());
app.use(
  teamRouter.allowedMethods({
    throw: true,
    notImplemented: () => new Error('notImplemented'),
    methodNotAllowed: () => new Error('methodNotAllowed')
  })
);

app.listen(8000, () => {
  console.log('success');
});

Checklist

zhaoyuqiqi commented 1 month ago

Define post method but throw an error when I use get method to request