{"id":13799603,"url":"https://github.com/gyson/koa-simple-router","last_synced_at":"2025-08-04T19:45:13.323Z","repository":{"id":57289464,"uuid":"45161671","full_name":"gyson/koa-simple-router","owner":"gyson","description":"Simple router for koa 2.x","archived":false,"fork":false,"pushed_at":"2015-11-17T18:14:24.000Z","size":27,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-19T05:50:48.319Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/gyson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-29T05:11:38.000Z","updated_at":"2021-06-02T14:28:25.000Z","dependencies_parsed_at":"2022-09-20T06:52:19.642Z","dependency_job_id":null,"html_url":"https://github.com/gyson/koa-simple-router","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/gyson/koa-simple-router","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyson%2Fkoa-simple-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyson%2Fkoa-simple-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyson%2Fkoa-simple-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyson%2Fkoa-simple-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gyson","download_url":"https://codeload.github.com/gyson/koa-simple-router/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gyson%2Fkoa-simple-router/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267030826,"owners_count":24024233,"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","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-08-04T00:01:04.378Z","updated_at":"2025-07-25T16:34:35.503Z","avatar_url":"https://github.com/gyson.png","language":"JavaScript","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"readme":"\n# koa-simple-router\n\n[![npm version](https://img.shields.io/npm/v/koa-simple-router.svg)](https://npmjs.org/package/koa-simple-router)\n[![build status](https://travis-ci.org/gyson/koa-simple-router.svg)](https://travis-ci.org/gyson/koa-simple-router)\n\nSimple and fast router for [koa](https://github.com/koajs/koa) 2.x\n\n## Features\n\n* support prefix\n* support auto OPTIONS and 405 response\n* use [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) to parse url\n* use express style routing ( `.get`, `.put`, `.post`, `.all`, etc )\n* use loop to iterate through multiple routes instead of recursive calls\n  * better performance\n  * prevent max call stack error with large number of routes\n\n## Installation\n\n```\n$ npm install koa-simple-router\n```\n\n## Usage\n\n```js\nconst Koa = require('koa') // koa 2.x\nconst router = require('koa-simple-router')\n\nlet app = new Koa()\n\napp.use(router(_ =\u003e {\n  _.get('/', (ctx, next) =\u003e {\n    ctx.body = 'hello'\n  })\n  _.post('/name/:id', (ctx, next) =\u003e {\n    // ...\n  })\n})\n```\n\n## API\n\n### `router(init)`\n\nCreate a router middleware with init function.\n\n```js\nconst Koa = require('koa')\nconst router = require('koa-simple-router')\nconst app = new Koa()\n\napp.use(router(_ =\u003e {\n  _.get('/', (ctx, next) =\u003e {\n\n  })\n  _.post('/path', (ctx, next) =\u003e {\n\n  })\n}))\n```\n\n### `router(options, init)`\n\nCreate a router middleware with options and init function.\n\nDefault options is the same as [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp).\n\n- **prefix** (default: `null`)\n- **sensitive** (default: `false`)\n- **strict** (default: `false`)\n- **end** (default: `true`)\n\n```js\nconst Koa = require('koa')\nconst router = require('koa-simple-router')\nconst app = new Koa()\n\napp.use(router({ prefix: '/api' }, _ =\u003e {\n  _.get('/:user/id', (ctx, next) =\u003e {\n\n  })\n  _.post('/:user/id', (ctx, next) =\u003e {\n\n  })\n}))\n```\n\n### `_.verb(path, ...mw)`\n\n```js\napp.use(router(_ =\u003e {\n  _.get('/path',\n    (ctx, next) =\u003e {},\n    (ctx, next) =\u003e {},\n    (ctx, next) =\u003e {}\n  )\n}))\n```\n\n### `_.all(path, ...[mw | obj])`\n\nMiddleware mode: works just like `_.verb(path, ...mw)` but ignore `ctx.method`\n\n```js\napp.use(router(_ =\u003e {\n  _.all('/path/to/:recource', (ctx, next) =\u003e {\n\n  })\n}))\n```\n\nObject mode: accept an object with method as key and middleware or array of middleware as value\n\n* auto `HEAD` response if `GET` present\n* auto `OPTIONS` response with `Allow` header\n* auto 405 response with `Allow` header\n\n```js\napp.use(router(_ =\u003e {\n  _.all('/path/to/:recource', {\n    get: (ctx, next) =\u003e {\n      // ...\n    },\n    post: (ctx, next) =\u003e {\n      // ...\n    },\n    put: (ctx, next) =\u003e {\n      // ...\n    },\n    delete: (ctx, next) =\u003e {\n      // ...\n    }\n    // Allow: GET, HEAD, POST, PUT, DELETE, OPTIONS\n  })\n}))\n```\n\nwhich is equivalent to\n\n```js\napp.use(router(_ =\u003e {\n  _.all('/path/to/:recource', (ctx, next) =\u003e {\n    switch (ctx.method) {\n      case 'GET':\n      case 'HEAD':\n        // ...\n        break\n      case 'POST':\n        // ...\n        break\n      case 'PUT':\n        // ...\n        break\n      case 'DELETE':\n        // ...\n        break\n      case 'OPTIONS':\n        ctx.status = 200\n        ctx.set('Allow', 'GET, HEAD, POST, PUT, DELETE, OPTIONS')\n        break\n      default:\n        ctx.status = 405 // method not allowed\n        ctx.set('Allow', 'GET, HEAD, POST, PUT, DELETE, OPTIONS')\n    }\n  }\n}))\n\n```\n\n### `_.param(param, ...mw)`\n\nRegister middleware for named route parameters.\n\n```js\napp.use(router(_ =\u003e {\n  _.param('user', async (ctx, next) =\u003e {\n    ctx.user = await User.find(ctx.params.user)\n    // ...\n    return next()\n  })\n\n  _.get('/:user/do-x', (ctx, next) =\u003e {\n\n  })\n\n  _.post('/:user/do-y', (ctx, next) =\u003e {\n\n  })\n}))\n\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyson%2Fkoa-simple-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgyson%2Fkoa-simple-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgyson%2Fkoa-simple-router/lists"}