{"id":17757,"library":"koa-router-middleware","title":"Koa Router Middleware","description":"This package provides a minimal router middleware for the Koa web framework, enabling the definition of HTTP routes and method-specific handlers. Published as `1.0.0-preview.1` approximately seven years ago (as of April 2026), this library represents an extremely early and likely experimental release. Due to its age, lack of subsequent updates, and preview versioning, `koa-router-middleware` is considered abandoned. Developers currently seeking robust and actively maintained routing solutions for Koa applications should instead utilize the well-established and official `@koa/router` package. This package's primary feature was its straightforward integration with Koa's `app.use()` method, allowing a router to be mounted at a specific path prefix.","status":"abandoned","version":"1.0.0-preview.1","language":"javascript","source_language":"en","source_url":"https://github.com/jameslnewell/koa","tags":["javascript","typescript"],"install":[{"cmd":"npm install koa-router-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add koa-router-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add koa-router-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"TypeScript type definitions for Koa, a peer dependency for type-safe Koa development.","package":"@types/koa","optional":true},{"reason":"The core Koa web framework, which this router middleware extends and requires to function.","package":"koa","optional":false}],"imports":[{"note":"The primary export is a default class, intended for ESM consumption. CommonJS `require` might not be supported or lead to issues with this older preview package.","wrong":"const Router = require('koa-router-middleware');","symbol":"Router","correct":"import Router from 'koa-router-middleware';"},{"note":"Instantiate the `Router` class to create a new router instance.","symbol":"new Router()","correct":"import Router from 'koa-router-middleware'; const router = new Router();"},{"note":"Route handlers are asynchronous functions following the Koa middleware signature, typically using `async/await`.","symbol":"Koa router instance methods (.get, .post, etc.)","correct":"router.get('/path', async ctx => { /* ... */ });"}],"quickstart":{"code":"import * as Koa from \"koa\";\nimport Router from \"koa-router-middleware\";\n\n// Define the API routes using koa-router-middleware\nconst apiRouter = new Router()\n  .use(async (ctx, next) => {\n    // Example middleware to set state, runs before route handlers\n    ctx.state.user = { name: \"James\" };\n    await next(); // Pass control to the next middleware or route handler\n  })\n  .get(\"/cat\", async ctx => {\n    ctx.status = 200;\n    ctx.body = [{ type: \"bengal\" }, { type: \"bombay\" }];\n  })\n  .get(\"/cat/:id\", async ctx => {\n    ctx.status = 200;\n    ctx.body = { type: \"bengal\", id: ctx.params.id };\n  })\n  .post(\"/cat\", async ctx => {\n    ctx.status = 201;\n    ctx.body = { type: \"siamese\" };\n  })\n  .put(\"/cat/:id\", async ctx => {\n    ctx.status = 200;\n    ctx.body = { type: \"siamese\", id: ctx.params.id };\n  })\n  .delete(\"/cat/:id\", async ctx => {\n    ctx.status = 200;\n    ctx.body = { message: `Cat ${ctx.params.id} deleted` };\n  });\n\n// Create a Koa application\nconst app = new Koa();\n\n// Use the API router, prefixed with \"/api\".\n// All routes defined in apiRouter will be accessible under /api/...\napp.use(\"/api\", apiRouter);\n\n// Start the server\nconst port = process.env.PORT ?? 3000;\napp.listen(port, () => {\n  console.log(`Server started on http://localhost:${port}`);\n  console.log('Try visiting:');\n  console.log(`- http://localhost:${port}/api/cat`);\n  console.log(`- http://localhost:${port}/api/cat/123`);\n  console.log('You can also test POST/PUT/DELETE requests to /api/cat or /api/cat/:id using tools like cURL or Postman.');\n});","lang":"typescript","description":"This quickstart demonstrates how to initialize a Koa application and register a `koa-router-middleware` instance with prefixed routes for various HTTP methods, including middleware for `ctx.state`."},"warnings":[{"fix":"Migrate to the actively maintained and widely adopted `@koa/router` package instead. This is the recommended and official routing solution for Koa applications.","message":"This package, `koa-router-middleware`, is effectively abandoned. It is an old preview release (`1.0.0-preview.1`) from 7 years ago with no further development or updates. Using it in production is highly discouraged.","severity":"breaking","affected_versions":">=1.0.0-preview.1"},{"fix":"If absolutely necessary to use this package, thoroughly test its behavior across your target Koa and Node.js versions. For any new development, refer to the fix for the 'breaking' warning above.","message":"Due to its 'preview' status and age, the API might not be stable, fully documented, or compatible with newer Koa features or Node.js versions beyond the specific Koa 2.x range it was designed for. While the peer dependency `koa: ^2.7.0` is stated, deeper incompatibilities may exist.","severity":"gotcha","affected_versions":">=1.0.0-preview.1"},{"fix":"Always double-check the exact package name (`koa-router-middleware`) when installing or importing to ensure you are aware you are using this specific, abandoned library.","message":"It is crucial not to confuse `koa-router-middleware` with the `koa-router` or `@koa/router` packages. This package is distinct and not part of the `koajs` official ecosystem, nor is it the popular community router.","severity":"gotcha","affected_versions":">=1.0.0-preview.1"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Ensure the package is correctly installed via `npm install koa-router-middleware` or `yarn add koa-router-middleware`. Verify the import statement for typos.","cause":"The package was not installed, or there's a typo in the import path/name.","error":"Error: Cannot find module 'koa-router-middleware'"},{"fix":"Use `import Router from 'koa-router-middleware';` for ESM environments. If in a CommonJS-only context, this package might not be directly usable without a transpilation layer, further highlighting its deprecated status.","cause":"Attempting to use `require()` for a module that is primarily designed for ESM `import` (default export).","error":"TypeError: Router is not a constructor"},{"fix":"Ensure your Koa version is compatible with the `app.use(path, middleware)` signature used in the `koa-router-middleware` examples. If issues persist, consider using a different Koa router like `@koa/router` which explicitly provides `router.routes()` and `router.allowedMethods()` for `app.use()`.","cause":"The `app.use(path, middleware)` signature for Koa (mounting a middleware with a path prefix) is specific to certain Koa versions or router implementations. If this router isn't properly wrapped or Koa doesn't support this form of `use` directly, it will fail.","error":"TypeError: app.use is not a function (when trying to mount a router directly like `app.use('/prefix', router)` with the wrong Koa version or setup)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}