{"id":15253526,"url":"https://github.com/bbqsrc/koa-rutt","last_synced_at":"2025-10-05T18:31:23.447Z","repository":{"id":66192638,"uuid":"42592920","full_name":"bbqsrc/koa-rutt","owner":"bbqsrc","description":"Another koa routing library","archived":true,"fork":false,"pushed_at":"2016-05-20T01:46:08.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-18T04:48:39.808Z","etag":null,"topics":["koa","koa-router","router"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bbqsrc.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},"funding":{"github":["bbqsrc"],"custom":"https://necessary.nu"}},"created_at":"2015-09-16T14:38:07.000Z","updated_at":"2023-01-28T14:30:10.000Z","dependencies_parsed_at":"2023-02-21T04:45:17.503Z","dependency_job_id":null,"html_url":"https://github.com/bbqsrc/koa-rutt","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"0a3908bc6716e4faf7b0de74f2f78d57686fa81f"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbqsrc%2Fkoa-rutt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbqsrc%2Fkoa-rutt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbqsrc%2Fkoa-rutt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bbqsrc%2Fkoa-rutt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bbqsrc","download_url":"https://codeload.github.com/bbqsrc/koa-rutt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235432243,"owners_count":18989483,"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":["koa","koa-router","router"],"created_at":"2024-09-29T21:05:20.857Z","updated_at":"2025-10-05T18:31:23.147Z","avatar_url":"https://github.com/bbqsrc.png","language":"JavaScript","readme":"# koa-rutt\n\n[![Build Status](https://travis-ci.org/bbqsrc/koa-rutt.svg?branch=master)](https://travis-ci.org/bbqsrc/koa-rutt) [![codecov.io](http://codecov.io/github/bbqsrc/koa-rutt/coverage.svg?branch=master)](http://codecov.io/github/bbqsrc/koa-rutt?branch=master)\n\nSwedish routing, for Koa.\n\n## Install\n\n`npm install koa-rutt`\n\n## Usage\n\nMost features will be demonstrated below.\n\n```javascript\nvar app = require('koa')();\n    Router = require('koa-rutt');\n\nvar router = new Router();\n\n// Add pre-middleware.\nrouter\n.pre(function*(next) {\n   // This will run on all HTTP methods first (GET, POST, etc);\n})\n.pre('post', function*(next) {\n  // Run a middleware just on this HTTP method (POST), such as a body parser\n})\n.get('/', function*(next) {\n  this.body = \"Index!\";\n})\n.get('/item/:id', function(next) {\n  // a `params` object is added to `this`.\n  var id = this.params.id;\n})\n.post('/item/:id', function(next) {\n  // You can use this just like koa-router, although repeating yourself\n  // does get annoying.\n})\n.route('/item2/:id', {\n  // How about we define them all at once?\n  get: function*(next) {\n    var id = this.params.id;\n  },\n  post: function*(next) {\n    // Oh this is much better!\n  }\n});\n\n// You can also return individual Route objects if you prefer\nrouter\n.route('/some/route')\n.get(function*(next) {\n  // ...\n})\n.post(function(next) {\n  // ...\n})\n.delete(function(next) {\n  // ...\n}),\n.put(function(next) {\n  // ...\n});\n\n// Add the router as middleware to the app.\napp.use(router.middleware());\n\napp.listen(3000);\n```\n\n## API\n\n- [callback `Middleware`](#callback-middleware)\n  - [`function* Middleware(ctx, next)`](#function-middlewarectx-next)\n- [class `Route`](#class-route)\n  - [`new Route(router, path)`](#new-routerouter-path)\n  - [`Route#get|post|delete|put(...middleware) → {Route}`](#routegetpostdeleteputmiddleware--route)\n- [class `Router`](#class-router)\n  - [`new Router([options])`](#new-routeroptions)\n  - [`Router#get|post|delete|put(path, ...middleware) → {Router}`](#routergetpostdeleteputpath-middleware--router)\n  - [`Router#use(routeClass) → {Router}`](#routeruserouteclass--router)\n  - [`Router#route(path, [methods]) → {Router|Route}`](#routerroutepath-methods--routerroute)\n  - [`Router#assets(path, realPath) → {Router}`](#routerassetspath-realpath--router)\n  - [`Router#pre([method], ...middleware) → {Router}`](#routerpremethod-middleware--router)\n  - [`Router#middleware() → {GeneratorFunction}`](#routermiddleware--generatorfunction)\n\n---\n\n### callback `Middleware`\n\n---\n\n#### `function* Middleware(ctx, next)`\n\nMiddleware callback signature for use with koa-rutt.\n\nMust be a generator.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| ctx | `KoaContext` |  | The koa context. |\n| next | `KoaMiddleware` |  | The koa 'next' middleware. |\n\n---\n\n### class `Route`\n\n---\n\n#### `new Route(router, path)`\n\nThe route.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| router | `Router` |  | The Router this Route is attached to. |\n| path | `String` |  | The URL path. |\n\n---\n\n#### `Route#get|post|delete|put(...middleware) → {Route}`\n\nAssign middleware to be run upon relevant HTTP method being triggered.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| middleware | `Middleware` | multiple | Middleware to be attached to called HTTP method. |\n\n**Returns:** `Route` Returns this instance of Route.\n\n---\n\n### class `Router`\n\n---\n\n#### `new Router([options])`\n\nThe router.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| [options] | `Object` | optional | Options object |\n| options.prefix | `string` |  | The prefix of each route of this router. |\n\n---\n\n#### `Router#get|post|delete|put(path, ...middleware) → {Router}`\n\nAssign middleware to be run upon relevant HTTP method being triggered.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| path | `String` |  | The path to the relevant Route. |\n| middleware | `Middleware` | multiple | Middleware to be attached to called HTTP method. |\n\n**Returns:** `Router` Returns this instance of Router.\n\n---\n\n#### `Router#use(routeClass) → {Router}`\n\nAdd routes using a provided constructor that implements the endpoints protocol.\n\nAll class methods are bound to the class instance.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| routeClass | `Function|Class` |  | The constructor implementing endpoints protocol |\n\n**Returns:** `Router` Returns this instance of Router.\n\n**Example:** Usage with class implementing endpoints protocol\n\n```javascript\nconst endpoints = require(\"koa-rutt\").endpoints\n\nclass MemberRoutes {\n  [endpoints]() {\n    return {\n      get: {\n        \"/member/:id\": this.get\n      },\n      post: {\n        \"/members\": this.create\n      },\n      put: {\n        \"/member/:id\": [requireAuth, this.update]\n      },\n      delete: {\n        \"/member/:id\": [requireAuth, this.delete]\n      }\n    }\n  }\n\n  * get(ctx, next) { ... }\n  * create(ctx, next) { ... }\n  * update(ctx, next) { ... }\n  * delete(ctx, next) { ... }\n}\n```\n\n---\n\n#### `Router#route(path, [methods]) → {Router|Route}`\n\nCreate or get a Route from the Router object, or HTTP methods on Route\nby using the optional methods parameter.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| path | `String` |  | The URL path to the resource. |\n| [methods] | `Object` | optional | An object with HTTP methods. |\n\n**Returns:** `Router|Route` Returns this instance of Router, or Route for path if no methods specified.\n\n**Example:** Usage with optional methods parameter\n\n```javascript\nrouter.route('/test', {\n  * get(ctx, next) {\n    // Do something\n  },\n  * post(ctx, next) {\n    // Do something\n  }\n})\n```\n\n**Example:** Usage with only path parameter\n\n```javascript\nconst testRoute = router.route('/test')\n```\n\n---\n\n#### `Router#assets(path, realPath) → {Router}`\n\nA convenience method for pointing all subpaths of path to a static assets\ndirectory and resolving content.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| path | `String` |  | The URL prefix for path to assets. |\n| realPath | `String` |  | The path to assets on local file system. |\n\n**Returns:** `Router` Returns this instance of Router.\n\n---\n\n#### `Router#pre([method], ...middleware) → {Router}`\n\nDefine middleware to run prior to HTTP method middleware. If no method\nprovided, the middleware will run before all other middlewares on the router.\n\n| Name | Type | Attributes | Description |\n| ---- | ---- | ---------- | ----------- |\n| [method] | `string` | optional | The HTTP method (eg 'get') to add pre-middleware to. |\n| middleware | `Middleware` | multiple | The middleware to attach. |\n\n**Returns:** `Router` Returns this instance of Router.\n\n**Example:** Example of #pre usage.\n\n```javascript\nrouter.pre(function* (ctx, next) {\n  ctx.type = 'application/json'\n  yield next\n}).pre('post', bodyParser())\n```\n\n---\n\n#### `Router#middleware() → {GeneratorFunction}`\n\nReturns the middleware to be provided to the Koa app instance.\n\n**Returns:** `GeneratorFunction` Middleware to provide to Koa.\n\n## License\n\nBSD 2-clause license. See LICENSE.\n","funding_links":["https://github.com/sponsors/bbqsrc","https://necessary.nu"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbqsrc%2Fkoa-rutt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbbqsrc%2Fkoa-rutt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbbqsrc%2Fkoa-rutt/lists"}