{"id":23404814,"url":"https://github.com/mohansandesh/koa-route-controllers","last_synced_at":"2026-02-16T17:02:12.180Z","repository":{"id":268216431,"uuid":"903650237","full_name":"mohansandesh/koa-route-controllers","owner":"mohansandesh","description":"MVC controller routing for Koa.js","archived":false,"fork":false,"pushed_at":"2025-01-25T05:24:22.000Z","size":59,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-29T20:48:53.749Z","etag":null,"topics":["koa","koa-router","koajs","mvc"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/koa-route-controllers","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/mohansandesh.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-15T07:07:54.000Z","updated_at":"2025-01-25T05:24:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"ba9b64c9-bf3f-4b50-8da0-6820be96046d","html_url":"https://github.com/mohansandesh/koa-route-controllers","commit_stats":null,"previous_names":["mohansandesh/koa-route-controllers"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/mohansandesh/koa-route-controllers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohansandesh%2Fkoa-route-controllers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohansandesh%2Fkoa-route-controllers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohansandesh%2Fkoa-route-controllers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohansandesh%2Fkoa-route-controllers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohansandesh","download_url":"https://codeload.github.com/mohansandesh/koa-route-controllers/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohansandesh%2Fkoa-route-controllers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29513433,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-16T09:05:14.864Z","status":"ssl_error","status_checked_at":"2026-02-16T08:55:59.364Z","response_time":115,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["koa","koa-router","koajs","mvc"],"created_at":"2024-12-22T13:15:53.303Z","updated_at":"2026-02-16T17:02:12.175Z","avatar_url":"https://github.com/mohansandesh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Koa Route Controllers\n\nMVC controller routing for *Koa.js*. Define app routes as MVC controller actions.\n\n## Features\n\n* MVC controller classes and actions\n* Before and after action filters\n* URL Helpers (From [koa-router](https://github.com/koajs/router), which is used internally for routing.)\n* Sub-apps\n\n## Example\n\nHere is an example of routes and a controller class. [More examples](#examples).\n\n```js\n// Koa app\nconst Koa = require('koa');\nconst KoaRouteControllers = require('koa-route-controllers');\nconst HelloController = require('./hello_controller.js');\n\n// Route Controllers\nconst routeControllers = new KoaRouteControllers()\n.get('/', HelloController, 'home')\n.get('/new', HelloController, 'new')\n.get('/show/:id', HelloController, 'show')\n.get('/edit/:id', HelloController, 'edit')\n.post('/create', HelloController, 'create')\n.patch('/update/:id', HelloController, 'update')\n.delete('/destroy/:id', HelloController, 'destroy');\n\n// Start Koa app\nconst app = new Koa();\napp.use(routeControllers.routes());\napp.listen(3000);\n```\n\n```js\n// Controller Class\nclass HelloController {\n  // home action\n  async home() {\n    this.ctx.body = {\n      hello: 'Koa'\n    };\n  }\n\n  // show action\n  async show() {\n    this.ctx.body = {\n      show: 'post'\n    };\n  }\n\n  // More actions\n}\n```\n\n## Install\n\n```\nnpm install koa-route-controllers\n```\n\n## API\n\n### KoaRouteControllers constructor\n\nThe constructor takes options specified [here](https://github.com/koajs/router/blob/master/API.md#new-routeropts).\n\nAdditionally it accepts an `appName` property. Which is used when creating [sub-app controllers](#examples). Default value is `main`.\n\n```js\n// examples\nconst routeControllers = new KoaRouteControllers();\nconst routeControllers = new KoaRouteControllers({ prefix: '/example' });\nconst routeControllers = new KoaRouteControllers({ host: 'hosta.com' });\nconst routeControllers = new KoaRouteControllers({ appName: 'sub-app-1' });\n```\n\n### KoaRouteControllers Instance\n\n`KoaRouteControllers` class instance provides the below http methods. These methods can be chained.\n\nEach of the http methods takes a `path` string, `ControllerClass` reference, `actionName` string and an optional `routeName` string. If `routeName` is null, it is automatically set to `controllerName_actionName`, all lowercase.\n\n`path` string can contain [URL parameters](https://github.com/koajs/router/blob/master/API.md#url-parameters) defined with a `:`, for example - `/show/:post_id/:comment_id`. Params can be accessed in actions with `this.params` (or `this.ctx.params`), for example `this.params.post_id` and `this.params.comment_id`. Querystring params can be accessed using `this.query` (or `this.ctx.query`).\n\n### get\n\nDefine a http `get` route.\n\n```js\nget(path, ControllerClass, actionName, routeName=null)\n```\n\n### post\n\nDefine a http `post` route.\n\n```js\npost(path, ControllerClass, actionName, routeName=null)\n```\n\n### put\n\nDefine a http `put` route.\n\n```js\nput(path, ControllerClass, actionName, routeName=null)\n```\n\n### patch\n\nDefine a http `patch` route.\n\n```js\npatch(path, ControllerClass, actionName, routeName=null)\n```\n\n### delete\n\nDefine a http `delete` route.\n\n```js\ndelete(path, ControllerClass, actionName, routeName=null)\n```\n\n### del\n\nAlias to `delete`.\n\n### Other methods\n\n### redirect\n\nDefine a `redirect`. Redirects with 301 status code by default.\n\n```js\nredirect(source, destination, code=null)\n```\n\n### routes\n\nReturns the routes middleware to be used with the Koa app.\n\n```js\n// Example\nconst app = new Koa();\napp.use(routeControllers.routes());\n```\n\n### router\n\nThis property contains the koa-router instance.\n\n## Controller Class\n\nFor every request, a matched controller class is automatically instantiated. Then the `action` instance method is called.\n\n### Before Action\n\nIf the controller class has an instance method `before`, it is called before calling the action method. This is useful, for example, when a controller action must be skipped.\n\nIf before action rendered or redirected, controller action and after action will not be called.\n\n```js\n// before action example\nclass PostsController {\n  async before() {\n    if(user == false){\n      // This will redirect and stop further execution of controller\n      this.ctx.redirect('/login');\n    }\n  }\n\n  // actions\n}\n```\n\n### After Action\n\nIf the controller class has an instance method `after`, it is called after calling the `action` method.\n\n```js\n// after action example\nclass PostsController {\n  async after() {\n    console.log(`${this.state.controllerName}#${this.state.actionName} has rendered`);\n  }\n\n  // actions\n}\n```\n\n### Properties\n\nThe following properties are available in controller actions.\n\n| Property    | Description |\n| ----------- | ----------- |\n| this.ctx    | ctx - Koa ctx |\n| this.state  | ctx.state - Koa ctx state  |\n| this.params | ctx.params - Current route params  |\n| this.query  | ctx.query - Current querystring params  |\n| this.url    | ctx.state.url - URL Helper function |\n\n### State\n\nThe following properties are available in `this.state`. `this.state` is just an alias of Koa `ctx.state`.\n\n| Property | Description |\n| -------- | ----------- |\n| this.state.controllerName | Current request controller name |\n| this.state.actionName | Current request action name |\n| this.state.routePath | Current request route path (Note: This is not the current URL) |\n| this.state.routeName | Current request route name |\n| this.state.url | URL helper function |\n| this.state.appName | Name of the app |\n| this.state.routerOpts | Options used in initializing this.router |\n| this.state.hasRendered | Flag if the request has rendered or redirected. It may mean the response is only set, but not sent yet. |\n\n### URL Helpers\n\nURL helper method `url` is available in `this.url` or `this.ctx.state.url`. For example - `this.url('controllerName_actionName', param1, param2, ...)`. In a view just `url(...)` will suffice, as `ctx.state` is available in views.\n\n[Here](https://github.com/koajs/router/blob/master/API.md#routerurlname-params-options--string--error) is the list of all options supported by the URL helper method.\n\nNote that the word `Controller` is removed from `controllerName`. An example URL would be `url('posts_show', 1)` for `PostsController#show`.\n\n## Notes\n\n### koa-router\n\n[koa-router](https://github.com/koajs/router) is used for actual routing.\n\nIf the `router` instance is needed for additional setup (ex: [allowedMethods](https://github.com/koajs/router/blob/master/API.md#routerallowedmethodsoptions--function)), it can be accessed using `routeControllersInstance.router`. koa-router API can be found [here](https://github.com/koajs/router/blob/master/API.md).\n\n### Controller Helpers\n\n[Here](https://github.com/mohansandesh/koa-route-controllers/blob/main/examples/blog/app/controllers/base_controller.js) is an example of a base controller that defines `render` helper.\n\n### App Directory Structure\n\nKoa Route Controllers doesn't require a particular app directory structure. Infact everything can be written in a [single js file](https://github.com/mohansandesh/koa-route-controllers/blob/main/examples/hello.js). \n\nBelow examples offer a way of organizing files.\n\nThe only optional convention is controller class name be `NameController` (ex: `PostsController`). This lets the route name automatically be set to `posts_actionname`, removing the word `Controller`. This also lets classes be identified as controllers.\n\n### Examples\n\nHere are some examples apps. To run these examples, clone the repo, run `npm install` and run `node examples/example-name`.\n\n| | |\n| --------- | ----- |\n| [Hello](https://github.com/mohansandesh/koa-route-controllers/blob/main/examples/hello.js) | A basic Hello example. |\n| [Blog](https://github.com/mohansandesh/koa-route-controllers/blob/main/examples/blog) | A MVC blog example with posts and comments. |\n| [Todo](https://github.com/mohansandesh/koa-route-controllers/blob/main/examples/todo.js) | An API only todo list app. |\n| [Sub-apps](https://github.com/mohansandesh/koa-route-controllers/blob/main/examples/sub-apps) | An example of a main app, which has two sub apps, sub-app-1 and sub-app-2. |\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohansandesh%2Fkoa-route-controllers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohansandesh%2Fkoa-route-controllers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohansandesh%2Fkoa-route-controllers/lists"}