{"id":24632689,"url":"https://github.com/mensu/koa-express-router","last_synced_at":"2026-04-30T22:35:32.023Z","repository":{"id":126590274,"uuid":"105436684","full_name":"Mensu/koa-express-router","owner":"Mensu","description":"Express's Router adapted for Koa v2.x","archived":false,"fork":false,"pushed_at":"2017-10-07T03:55:22.000Z","size":84,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-25T20:38:04.894Z","etag":null,"topics":["express","koa","koa-router","koa2","router"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/koa-express-router","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mensu.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-10-01T11:11:47.000Z","updated_at":"2021-03-06T15:37:21.000Z","dependencies_parsed_at":"2023-06-17T08:45:35.809Z","dependency_job_id":null,"html_url":"https://github.com/Mensu/koa-express-router","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/Mensu/koa-express-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mensu%2Fkoa-express-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mensu%2Fkoa-express-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mensu%2Fkoa-express-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mensu%2Fkoa-express-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mensu","download_url":"https://codeload.github.com/Mensu/koa-express-router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mensu%2Fkoa-express-router/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262850261,"owners_count":23374355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["express","koa","koa-router","koa2","router"],"created_at":"2025-01-25T08:12:33.991Z","updated_at":"2026-04-30T22:35:27.001Z","avatar_url":"https://github.com/Mensu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-express-router\n\n\u003e Express's Router adapted for [Koa](http://koajs.com) v2.x\n\n[![NPM version](http://img.shields.io/npm/v/koa-express-router.svg?style=flat)](https://npmjs.org/package/koa-express-router) [![node version](https://img.shields.io/node/v/koa-express-router.svg?style=flat)](https://www.npmjs.com/package/koa-express-router) [![NPM Downloads](https://img.shields.io/npm/dm/koa-express-router.svg?style=flat)](https://npmjs.org/package/koa-express-router) [![Build Status](https://travis-ci.org/Mensu/koa-express-router.svg?branch=master)](https://travis-ci.org/Mensu/koa-express-router) [![License](http://img.shields.io/npm/l/koa-express-router.svg?style=flat)](LICENSE)\n\n* Express-style routing using `router.use`, `router.all`, `router.METHOD`, `router.param` etc\n* Support router prefix\n* Support query matching\n\n## Thanks To\n\n- [Express](https://expressjs.com). This repo is based on the codes on [expressjs/express](https://github.com/expressjs/express)\n- [path-to-regexp](https://github.com/pillarjs/path-to-regexp)\n\n## Installation\n\n```sh\nnpm install koa-express-router\n```\n\n## Usage\n\n### Basics\n\nJust like ``express``\n\n##### Differences\n\n- use ``new`` to create instances\n- use ``.routes()`` or ``.routes(false)`` to export a router\n\n```js\nconst Koa = require('koa');\nconst Router = require('koa-express-router');\n\n// change default settings\nRouter.defaultOptions.mergeParams = true;\n\n// define sub router with an optional prefix\n// NOTE: different from Express: use new to create instances\nconst subRtr = new Router({ prefix: '/sub' });\n\nsubRtr.use(async (ctx, next) =\u003e {\n  if (/* some condition */false) {\n    return next('router');  // possible to skip the current router\n  }\n  ctx.body += '/sub use \\n';\n  return next();\n});\n\nsubRtr.route('/list')\n  .all(async (ctx, next) =\u003e {\n    if (/* some condition */false) {\n      return next('route');  // possible to skip the current route (goto #1)\n    }\n    ctx.body += '/sub/list all 1\\n';\n    return next();\n  })\n  .get(async (ctx, next) =\u003e {\n    ctx.body += '/sub/list get\\n';\n  })\n  .post(async (ctx, next) =\u003e {\n    ctx.body += '/sub/list post\\n';\n  });\n\nsubRtr.all('/list', async (ctx, next) =\u003e {\n  // #1\n  // the logic would come here\n  ctx.body += '/sub/list all 2\\n';\n});\n\n// define top router without a prefix\nconst topRtr = new Router();\ntopRtr.use((ctx, next) =\u003e {\n  ctx.body = 'global use\\n';\n  return next();\n});\ntopRtr.param('someID', async (ctx, next, id, name) =\u003e {\n  // id: matched value\n  // name: 'someID'\n  // ...\n  ctx.body += `/top/:someID ${name} =\u003e ${id}\\n`;\n  return next();\n});\n\n// NOTE: different from Express: use .routes() to export\n// no need to pass arguments to .routes()\n//   if .routes() is to be used by a Router from 'koa-express-router'\ntopRtr.use('/top/:someID([1-9]{1}[0-9]{0,})/', subRtr.routes());\n\nconst app = new Koa();\n// pass 'false' when .routes() may be used\n//   by app.use, compose, or something other than a Router from 'koa-express-router'\napp.use(topRtr.routes(false));\napp.listen(3000);\n\n```\n\n### Output\n\n- GET /\n  ```\n  global use\n\n  ```\n- PATCH /top/10/sub/ (does not match any path in subRtr)\n  ```\n  global use\n  /top/:someID someID =\u003e 10\n  /sub use\n\n  ```\n- GET /top/bad/sub/list (does not match the use for subRtr.routes())\n  ```\n  global use\n\n  ```\n- GET /top/2/sub/list\n  ```\n  global use\n  /top/:someID someID =\u003e 2\n  /sub use\n  /sub/list all 1\n  /sub/list get\n\n  ```\n- PUT /top/3/sub/list\n  ```\n  global use\n  /top/:someID someID =\u003e 3\n  /sub use\n  /sub/list all 1\n  /sub/list all 2\n\n  ```\n\n### Query Matching\n\n**Note:** this feature is only designed for simple matching. If the matching condition becomes complex, it is recommended that the user consider ``return next('route')`` for better readability. See below.\n\n```js\n// use an object for query matching schema\n// the value part can be a primitive\nrouter.use({ type_id: 1, state: 'good' }, async (ctx, next) =\u003e {\n  // matched on ?type_id=1\u0026state=good\n  return next();\n});\n\nrouter.route('/list')\n  // the value part can be a function\n  .get({ user_id: val =\u003e Number(val) \u003c 1000 }, async (ctx, next) =\u003e {\n    // case ?user_id=30\n    // ...\n    // use next('route') to break (like break in switch-case statements), if needed\n    return next('route');\n  })\n  // the value part can be a regexp\n  .get({ user_id: /00$/ }, async (ctx, next) =\u003e {\n    // case ?user_id=3000\n    // ...\n    // use next('route') to break\n    return next('route');\n  })\n  // the value part can be a string array (meaning or)\n  .get({ user_id: ['2222', '3333'] }, async (ctx, next) =\u003e {\n    // case ?user_id=2222 or ?user_id=3333\n    // ...\n    // use next('route') to break\n    return next('route');\n  })\n  .get(async (ctx, next) =\u003e {\n    // default case\n    return next('route');\n  });\n\n// complex query condition example\nrouter.post('/',\n  async (ctx, next) =\u003e {\n    const authorized = await checkAuthorized(ctx.query.user_id);\n    if (authorized) return next(); // goto #1\n    return next('route'); // goto #2\n  },\n  async (ctx, next) =\u003e {\n    // #1\n    // authorized operations\n  },\n);\n\nrouter.post('/',\n  async (ctx, next) =\u003e {\n    // #2\n    // limited operations due to not being authorized\n  },\n);\n\n```\n\n## Caveats\n\n- Not ready for production use\n- \u003cs\u003eNot support other less common HTTP methods\u003c/s\u003e\n- Not benchmarked\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmensu%2Fkoa-express-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmensu%2Fkoa-express-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmensu%2Fkoa-express-router/lists"}