{"id":17956244,"url":"https://github.com/buunguyen/route-decorators","last_synced_at":"2025-10-30T03:12:00.738Z","repository":{"id":66231009,"uuid":"46521309","full_name":"buunguyen/route-decorators","owner":"buunguyen","description":"ES7 decorators that simplify Koa and Express route creation","archived":false,"fork":false,"pushed_at":"2020-04-28T19:17:37.000Z","size":10,"stargazers_count":75,"open_issues_count":2,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-19T07:03:40.468Z","etag":null,"topics":["decorators","es7-decorators","koa","routes"],"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/buunguyen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"github":["buunguyen"]}},"created_at":"2015-11-19T21:24:35.000Z","updated_at":"2024-04-19T08:07:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"ce045eb9-6938-44dd-98f4-eb8cd29a3297","html_url":"https://github.com/buunguyen/route-decorators","commit_stats":{"total_commits":12,"total_committers":3,"mean_commits":4.0,"dds":"0.16666666666666663","last_synced_commit":"91cbf91a439ed52dfea7b250f04e32ee23e3e461"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Froute-decorators","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Froute-decorators/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Froute-decorators/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Froute-decorators/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buunguyen","download_url":"https://codeload.github.com/buunguyen/route-decorators/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245385359,"owners_count":20606648,"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":["decorators","es7-decorators","koa","routes"],"created_at":"2024-10-29T10:36:55.587Z","updated_at":"2025-10-30T03:11:55.711Z","avatar_url":"https://github.com/buunguyen.png","language":"JavaScript","funding_links":["https://github.com/sponsors/buunguyen"],"categories":[],"sub_categories":[],"readme":"## route-decorators for Koa/Express\n\n[![NPM](https://nodei.co/npm/route-decorators.png?compact=true)](https://www.npmjs.com/package/route-decorators)\n\n[![Build Status](https://travis-ci.org/buunguyen/route-decorators.svg?branch=master)](https://travis-ci.org/buunguyen/route-decorators)\n\n[ES7 decorators](https://github.com/wycats/javascript-decorators) that simplify Koa and Express route creation. Using these decorators, you can write your controllers like below and have all the routes populated.\n\n__Koa__\n```js\nimport {controller, get, post} from 'route-decorators'\n\n@controller('/users', middleware1)\nclass UserCtrl {\n\n  @get('/:id', middleware2, middleware3)\n  async get(context, next) {}\n\n  @post(middleware2)\n  async post(context, next) {}\n}\n```\n\n__Express__\n```js\nimport {controller, get, post} from 'route-decorators'\n\n@controller('/users', middleware1)\nclass UserCtrl {\n\n  @get('/:id', middleware2, middleware3)\n  async get(req, res, next) {}\n\n  @post(middleware2)\n  async post(req, res, next) {}\n}\n```\n\nOnce the decorators are applied, every controller instance will receive a `$routes` array, which you can use to define actual Koa/Express routes.\n\nAssume the above `UserCtrl` definition, you can define routes in `UserCtrl`'s constructor (although really you can put the code anywhere) as follows:\n\n__Koa__\n```js\nimport Router from 'koa-66'\n\n// Inside controller constructor\nthis.router = new Router()\nfor (const {method, url, middleware, fnName} of this.$routes) {\n  this.router[method](url, ...middleware, this[fnName].bind(this))\n}\n```\n\n__Express__\n```js\nimport express from 'express'\n\n// Inside controller constructor\nthis.router = express.Router()\nfor (const {method, url, middleware, fnName} of this.$routes) {\n  this.router[method](url, ...middleware, (req, res, next) =\u003e {\n    this[fnName](req, res, next).catch(next)\n  })\n}\n```\n\nYou can move the above logic to some base controller in your app and reuse it for every controller. For example:\n\n```js\nclass BaseCtrl {\n  constructor() {\n    this.router = new Router()\n    for (const {method, url, middleware, fnName} of this.$routes) {\n      this.router[method](url, ...middleware, this[fnName].bind(this))\n    }\n  }\n}\n\n@controller(...)\nclass UserCtrl extends BaseCtrl {\n  // decorated methods as above\n}\n```\n\n### Decorators\n * `@controller(path: optional, ...middleware: optional)`\n * `@route(method, path: optional, ...middleware: optional)`\n * `@head`, `@options`, `@get`, `@post`, `@put`, `@patch`, `@del`, `@delete`, `@all`: wrappers of `@route` that automatically supply the `method` argument.\n\n### Test\n\n```bash\nnpm install\nnpm test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuunguyen%2Froute-decorators","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuunguyen%2Froute-decorators","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuunguyen%2Froute-decorators/lists"}