{"id":13799555,"url":"https://github.com/ivpusic/koa-routing","last_synced_at":"2025-06-28T04:03:49.773Z","repository":{"id":15383992,"uuid":"18115525","full_name":"ivpusic/koa-routing","owner":"ivpusic","description":"Manage koa routes on right way","archived":false,"fork":false,"pushed_at":"2015-05-30T17:56:42.000Z","size":405,"stargazers_count":15,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-13T08:44:10.305Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivpusic.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}},"created_at":"2014-03-25T20:29:13.000Z","updated_at":"2021-08-10T10:56:34.000Z","dependencies_parsed_at":"2022-08-25T18:02:06.129Z","dependency_job_id":null,"html_url":"https://github.com/ivpusic/koa-routing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ivpusic/koa-routing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivpusic%2Fkoa-routing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivpusic%2Fkoa-routing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivpusic%2Fkoa-routing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivpusic%2Fkoa-routing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivpusic","download_url":"https://codeload.github.com/ivpusic/koa-routing/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivpusic%2Fkoa-routing/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262371664,"owners_count":23300593,"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-08-04T00:01:03.940Z","updated_at":"2025-06-28T04:03:49.755Z","avatar_url":"https://github.com/ivpusic.png","language":"JavaScript","funding_links":[],"categories":["仓库"],"sub_categories":["中间件"],"readme":"koa-routing\n================\n[![Build Status](https://travis-ci.org/ivpusic/koa-routing.svg?branch=master)](https://travis-ci.org/ivpusic/koa-routing)\n[![Dependency Status](https://gemnasium.com/ivpusic/koa-routing.svg)](https://gemnasium.com/ivpusic/koa-routing)\n## Installation\n```\nnpm install koa-routing\n```\n\n## Motivation\n\nI wanted to separate my route definitions into multiple files. Also I wanted to make easier to specify route handlers, and execute some methods before some set of routes, for example ensuring that user is authenticated before doing some action. So I developed [koa-r](https://github.com/ivpusic/koa-r) and [koa-routing](https://github.com/ivpusic/koa-routing) to achieve that. Final result is something like this:\n\n**/routing/index.js** file\n```\nmodule.exports = function (app) {\n  require('./users')(app.route('/api/users').before(authenticate));\n};\n```\n\n**/routing/users.js** file\n```\n/**\n * /api/users\n */\n\nmodule.exports = function (route) {\n  /* GET /api/users */\n  route.get(r('user', 'getUsers'));\n\n  /* GET /api/users/logout */\n  route.nested('/logout').get(r('user', 'logout'));\n};\n```\n\nSo here you can see that we are specifying handlers for route with ``r('module', 'method')`` pattern, and we are also following DRY principle when we define our routes.\n\nIf you like this idea, you are on right place.\n\n### Example\n\nLet's define following routes:\n- ``/users`` [GET, POST, PUT],\n- ``/users/list`` [GET, PUT]\n\nWith ``koa-routing`` you can nest routes, and on that way you can follow DRY principle.\nAlso ``koa-routing`` architecture help you to separate route handlers into multiple files. That example will be shown also.\n\n```\nvar koa = require('koa'),\n\trouting = require('koa-routing');\n\nvar app = koa();\napp.use(routing(app));\n\napp.route('/users')\n  .get(function * (next) {\n    this.body = 'from get';\n    yield next;\n  })\n  .post(function * (next) {\n    this.body = 'from post';\n    yield next;\n  })\n  .put(function * (next) {\n    this.body = 'from put';\n    yield next;\n  })\n  .nested('/list')\n    .get(function * (next) {\n      this.body = 'from users list GET';\n      yield next;\n    });\n    .put(function * (next) {\n      this.body = 'from users list PUT';\n      yield next;\n    });\n\napp.listen(4000);\n```\n**You should put ``koa-routing`` middleware after body parsers and simmilar middlewares which are preparing request for you, or passing an options object with a ``defer`` field setted to ``true``**.\n\nAs you can see, you can pass classic ``express`` route style, such as ``/user/:id``, and after that you can read received values from ``this.params`` or ``this.request.params`` object.\n\nYou can pass also regex as route path.\n\n## API\n\n#### route\n``koa-routing`` extends you application instance with ``route`` method.\nYou can use that method for defining route path.\n\n```\napp.route('/users/:id');\n```\n\n#### HTTP methods\n\nAfter you define your route, you need set ``HTTP`` methods for that route.\nIn following example you need to replace ``someHTTPmethod`` with one of supported\n``node`` ``HTTP`` methods. That can be ``GET``, ``POST``, ``PUT``, etc...\n\n```\napp.route('route path').someHTTPmethod(handler);\n```\n\nSo you can type something like:\n```\nvar handler = function * () {\n  yield next;\n};\n\napp.route('api/users').get(handler);\n```\n\nKeep in mind that every call returns router instance, so everything can be chained.\n\n#### nested\n\nLet's we say that you have for routes something like this:\n- ``/api/users/profile/data``\n- ``/api/users/profile/image``\n- etc.\n\nYou see that you are repeating ``/api/users/profile`` for every route, and we don't want to do that.\n``koa-routing`` have nice solution for this with ``nested`` function.\n\n```\n// first you type fixed part\nvar route = app.route('/api/users/profile');\n\nroute.nested('/data')\n  .get(function * (next) { yield next; });\n  // here you can also define other HTTP operations, like POST, PUT, etc\n  // example of put...\n  .put(function * (next) { yield next; });\n\nroute.nested('/image')\n  .get(function * (next) { yield next; });\n```\n\nKeep in mind that nested creates new route for you and returns created route. You can continue nesting routes. It is up to you.\n\n#### before\n\nYou can define function which will be executed before each route method, and before all nested routes.\n```\napp.route('/someRoute')\n\t.before(function * (next) {\n\t\tthis.status = 300;\n\t})\n\t.get(function * (next) {\n\t\tthis.body = 'should not be here';\n\t\tthis.status = 200;\n\t\tyield next;\n\t});\n```\n\n#### all\n\nThis function will be executed if there is no matching HTTP method.\n```\napp.route('/someRoute')\n\t.all(function * (next) {\n\t\tthis.body = 'will catch GET/POST/PUT... etc';\n\t\tthis.status = 200;\n\t\tyield next;\n\t})\n```\n\n## Other features\n\n#### Multiple middlewares\n\nWith ``koa-routing`` you can provide multiple middlewares for each route method:\n```\napp.route('/multipleMiddleware')\n\t.get(function * (next) {\n\t\tthis.body = '1';\n\t\tthis.status = 200;\n\t\tyield next;\n\t}, function * (next) {\n\t\tthis.body = '2';\n\t\tyield next;\n\t});\n```\n\nIf you go to this route you will receive ``2`` as a result, because request will be passed\nto each defined handler.\n\n#### Options\n\n```\napp.use(routing(app,options));\n```\n\n* ``defer`` Default is false. If true, serves after yield next, allowing any downstream middleware to respond first.\n\n## Contributing\n\nFeel free to send pull request with some new awesome feature or some bug fix.\nBut please provide some tests with your contribution.\n\n# License\n**MIT**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivpusic%2Fkoa-routing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivpusic%2Fkoa-routing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivpusic%2Fkoa-routing/lists"}