{"id":16326691,"url":"https://github.com/chrisyip/koa-power-router","last_synced_at":"2025-10-25T20:32:12.064Z","repository":{"id":25216715,"uuid":"28640891","full_name":"chrisyip/koa-power-router","owner":"chrisyip","description":"Yet another router middleware for Koa, but better.","archived":false,"fork":false,"pushed_at":"2015-04-24T18:34:31.000Z","size":179,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-31T10:36:03.752Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/chrisyip.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":"2014-12-30T18:25:47.000Z","updated_at":"2017-06-30T08:51:42.000Z","dependencies_parsed_at":"2022-09-08T13:53:00.166Z","dependency_job_id":null,"html_url":"https://github.com/chrisyip/koa-power-router","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-power-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-power-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-power-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisyip%2Fkoa-power-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisyip","download_url":"https://codeload.github.com/chrisyip/koa-power-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238207648,"owners_count":19434095,"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":[],"created_at":"2024-10-10T23:09:18.661Z","updated_at":"2025-10-25T20:32:06.765Z","avatar_url":"https://github.com/chrisyip.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# koa-power-router\n\n[![Node version][node-image]][npm-url] [![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Travis CI][travis-image]][travis-url] [![Coveralls][coveralls-image]][coveralls-url]\n\nFeatures:\n\n- `onSTATUS` handler (globally or per controller)\n- `beforeAction`: a function list that runs before router action, if any of them return values except `undefined` or `null` will skip action, and go to `onSTATUS` handler\n\n# How to use\n\nInstall:\n\n```shell\nnpm install --save koa-power-router\n```\n\nUsage:\n\n```js\nvar koa = require('koa')\nvar app = koa()\nvar router = require('koa-power-router/router')\nvar Controller = require('koa-power-router/controller')\n\nvar options = {\n  strictSlash: true // or false\n}\n\napp.use(router(options))\n\nrouter.on('404', function* () {\n  yield this.render('404')\n})\n\n// Functions and Generators will auto-convert to Controller\nrouter.get('/', function* (next) {\n  yield this.render('homepage')\n  yield next\n})\n\n// Define a Controller with beforeAction\nvar user = new Controller({\n  beforeAction: function () {\n    // if not login and token is empty\n    if (this.url.indexOf('/login') !== 0 \u0026\u0026\n        !this.cookies.get('token')) {\n      return true\n    }\n  },\n  login: function* () {\n    if (this.method === 'get') {\n      yield this.render('login')\n    } else {\n      doLogin()\n    }\n  },\n  dashboard: function* () {\n    yield this.render('dashboard')\n  },\n  on404: function* () {\n    yield this.render('user/404')\n  }\n})\n\nrouter.set('/login', ['get', 'post'], user.login)\nrouter.get('/dashboard', user.dashboard)\n\nrouter.on('404', function* () {\n  yield this.render('404')\n})\n```\n\n# Router\n\n## Methods\n\n### router.configure(options)\n\n```js\napp.use(router())\nrouter.configure({})\n// equals to\napp.use(router({}))\n```\n\nSupported options:\n\n#### strictSlash\n\n\u003e HTTP RFC 2396 defines path separator to be single slash.\n\n\u003e However, unless you're using some kind of URL rewriting (in which case the rewriting rules may be affected by the number of slashes), the uri maps to a path on disk, but in (most?) modern operating systems (Linux/Unix, Windows), multiple path separators in a row do not have any special meaning, so /path/to/foo and /path//to////foo would eventually map to the same file.\n\n[http://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything](http://stackoverflow.com/questions/10161177/url-with-multiple-forward-slashes-does-it-break-anything)\n\nIf `strictSlash` is `true`, URL like `//demo` will not match the following router:\n\n```js\nrouter.get('/demo', function () {})\n```\n\n### router.set(url, methods, action)\n\n`url`: the pattern of URL, convertd to regular expression with [path-to-regexp](https://github.com/pillarjs/path-to-regexp).\n\n`url` supports:\n\n- String: `'/homepage'`\n- Named string: `'/uesr/:id'`\n- Regular expression: `/^/item/(\\d)+/i`\n\n`methods`: the HTTP methods that this router should support, can be `string` and `array`.\n\n`action`: `function` or `generator`.\n\n#### Shortcuts\n\n- `router.all(url, action)`\n- `router.get(url, action)`\n- `router.post(url, action)`\n- `router.put(url, action)`\n- `router.delete(url, action)`\n\n### router.on(status, handler)\n\nRegister a handler for specified status.\n\n```js\nrouter.on('500', function* (next, error) {\n  yield this.render('500')\n})\n```\n\nWith this feature, you can render error page without redirecting to an actual error page.\n\n# Controller\n\nAn exmaple of how to define a controller:\n\n```js\nvar user = new Controller({\n  beforeAction: function () {\n    if (!this.cookies.get('token')) {\n      return true\n    }\n  },\n  dashboard: function* () {\n    yield this.render('dashboard')\n  },\n  on404: function* (){\n\n  }\n})\n```\n\n## Action\n\nSame as the Koa middleware, nothing different.\n\n```js\ndashboard: function* (next) {\n  yield this.render('dashboard')\n  yield next\n}\n```\n\n## Methods\n\n### on(status, handler)\n\nRegister a handler for specified status.\n\n```js\nuser.on('500', function* () {\n  yield this.render('500')\n})\n```\n\n**If a status is handled by a controller, router's handler will be skipped.**\n\nWith this feature, you can render error page without redirecting to an actual error page.\n\n## Properties\n\n### beforeAction\n\nA list of function that run before controller's action. For example:\n\n```js\nar user = new Controller({\n  beforeAction: function () {\n    if (!this.cookies.get('token')) {\n      this.status = 401\n      return true\n    }\n  },\n  dashboard: function* () {\n    yield this.render('dashboard')\n  },\n  on401: function* () {\n    yield this.render('user/not-authorized')\n  }\n})\n\nrouter.set('/login', ['get', 'post'], user.login)\nrouter.get('/dashboard', user.dashboard)\n```\n\nAny request of `/dashboard` without `token` will go to `on401` handler and render `user/not-authorized` page instead `dashboard` page.\n\n# Yieldables\n\nPower router uses [`yieldr`](https://github.com/chrisyip/yieldr) to yield generatos and promises, you should notice that `yieldr` is slightly different from [`co`](https://github.com/tj/co).\n\nIf you really need some features that provided by `co`, use `co` directly:\n\n```js\nvar co = require('co')\n\nrouter.get('/', function* () {\n  var result = yield co(function* () {\n    return yield [Promise.resolve('foo')]\n  })\n  console.log(result) // [ 'foo' ]  \n})\n```\n\nOr:\n\n```js\nrouter.get('/', function* () {\n  var result = [yield Promise.resolve('foo')]\n  console.log(result) // [ 'foo' ]\n})\n```\n\n# Contributors\n\nVia [GitHub](https://github.com/chrisyip/koa-power-router/graphs/contributors)\n\n[node-image]: http://img.shields.io/node/v/koa-power-router.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-power-router\n[npm-image]: http://img.shields.io/npm/v/koa-power-router.svg?style=flat-square\n[daviddm-url]: https://david-dm.org/chrisyip/koa-power-router\n[daviddm-image]: http://img.shields.io/david/chrisyip/koa-power-router.svg?style=flat-square\n[travis-url]: https://travis-ci.org/chrisyip/koa-power-router\n[travis-image]: http://img.shields.io/travis/chrisyip/koa-power-router.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/chrisyip/koa-power-router\n[coveralls-image]: http://img.shields.io/coveralls/chrisyip/koa-power-router.svg?style=flat-square\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisyip%2Fkoa-power-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisyip%2Fkoa-power-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisyip%2Fkoa-power-router/lists"}