{"id":16550847,"url":"https://github.com/tmaize/koa-annotation-router","last_synced_at":"2026-05-09T13:39:52.809Z","repository":{"id":134082848,"uuid":"412947773","full_name":"TMaize/koa-annotation-router","owner":"TMaize","description":"koa注解风格路由中间件，基于@koa/router","archived":false,"fork":false,"pushed_at":"2021-11-18T16:34:49.000Z","size":24,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-15T12:06:30.638Z","etag":null,"topics":["annotation","koa","middleware","router"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TMaize.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-10-03T01:18:41.000Z","updated_at":"2023-11-30T19:25:32.000Z","dependencies_parsed_at":"2024-02-06T09:00:14.994Z","dependency_job_id":null,"html_url":"https://github.com/TMaize/koa-annotation-router","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TMaize%2Fkoa-annotation-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TMaize%2Fkoa-annotation-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TMaize%2Fkoa-annotation-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TMaize%2Fkoa-annotation-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TMaize","download_url":"https://codeload.github.com/TMaize/koa-annotation-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241871390,"owners_count":20034452,"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":["annotation","koa","middleware","router"],"created_at":"2024-10-11T19:35:33.670Z","updated_at":"2026-05-09T13:39:52.762Z","avatar_url":"https://github.com/TMaize.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-annotation-router\n\n拓展 [@koa/router](https://github.com/koajs/router)，使其支持注解风格路由写法\n\n```js\nimport { Router, controller, mapping } from 'koa-annotation-router'\n```\n\n`controller` 和 `mapping` 用于装饰类和方法定义访问路径\n\n`Router` 继承自 `@koa/router` 增加了 `addController` 方法用于注册被装饰的类，增加了 `printRoutes` 方法用于打印所有注册的路由\n\n```\n┌─────────┬────────┬────────────────────┬──────────────────────────────┐\n│ (index) │ method │        path        │            caller            │\n├─────────┼────────┼────────────────────┼──────────────────────────────┤\n│    0    │ 'GET'  │ '/api/user/hello'  │ 'new UserController().hello' │\n│    1    │ 'POST' │ '/api/user/update' │   'UserController.update'    │\n│    2    │ 'GET'  │      '/hello'      │              ''              │\n└─────────┴────────┴────────────────────┴──────────────────────────────┘\n```\n\n## 安装\n\n注意：不需要再安装 `@koa/router` 或者 `koa-router` 了\n\n```bash\nyarn add koa-annotation-router\n```\n\n## 使用\n\n参考 [test/index.ts](./test/index.ts)，[test/demo.ts](./test/demo.ts)\n\n```ts\nimport Koa, { Context } from 'koa'\nimport { Router, controller, mapping } from 'koa-annotation-router'\n\n@controller('/api/user')\nclass UserController {\n  name: string\n  constructor(name: string) {\n    this.name = name\n  }\n\n  @mapping('/hello')\n  public async hello(ctx: Context) {\n    ctx.response.body = `hello ${this.name}`\n  }\n\n  @mapping('/update', ['post'])\n  public static async update(ctx: Context) {\n    ctx.response.body = {\n      code: 200,\n      data: null,\n      msg: 'ok'\n    }\n  }\n}\n\nconst router = new Router()\nrouter.addController(new UserController('tom'))\nrouter.get('/hello', async function (ctx: Context) {\n  ctx.response.body = `hello`\n})\n\nconst app = new Koa().use(router.routes())\napp.listen(3000)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftmaize%2Fkoa-annotation-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftmaize%2Fkoa-annotation-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftmaize%2Fkoa-annotation-router/lists"}