Skip to content

[fix] allowedMethods will fail if two routes are defined #182

Closed
@lbesson-della

Description

@lbesson-della

Describe the bug

In commit f7ffb93

if (ctx.matched) {
  ctx.matched.push.apply(ctx.matched, matched.path);
} else {
  ctx.matched = matched.path;
}

has been changed to

if (ctx.matched) {
  ctx.matched.push(matched.path);
} else {
  ctx.matched = matched.path;
}

This causes an issue with allowedMethods if more than a router is defined

Actual behavior

TypeError: Cannot read properties of undefined (reading 'length')
    at D:\code\legisway-connector\node_modules\@koa\router\lib\router.js:330:47

Expected behavior

The code should not fail

Code to reproduce

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

const app = new Koa();
const router1 = new Router();
const router2 = new Router();
router1.get('/foo', ctx => {
  ctx.body = 'foo';
});
router2.get('/bar', ctx => {
  ctx.body = 'bar';
});
app.use(router1.routes());
app.use(router1.allowedMethods({ throw: true }));
app.use(router2.routes());
app.use(router2.allowedMethods({ throw: true }));
app.listen(8888);

$ curl  localhost:8888

Checklist

  • I have searched through GitHub issues for similar issues.
  • I have completely read through the README and documentation.
  • I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions