koajs / router

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

fix: undefined property in unmatched host #178

Closed laverdet closed 3 weeks ago

laverdet commented 5 months ago

allowedMethods() throws a runtime error when the host of its router fails to match. Additionally it would throw if you never added routes() to your koa middleware chain.

Example:

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

const app = new Koa();
const router = new Router({ host: "aaaa" });
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(8888);
$ curl localhost:8888/                   
Internal Server Error

Error:

  TypeError: Cannot read properties of undefined (reading 'length')
      at /Users/marcel/braid/web/.pnpm/@koa+router@12.0.1/node_modules/@koa/router/lib/router.js:487:41
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Checklist