{"id":13657345,"url":"https://github.com/koajs/trie-router","last_synced_at":"2025-05-06T21:30:05.584Z","repository":{"id":11859014,"uuid":"14417887","full_name":"koajs/trie-router","owner":"koajs","description":"Trie-routing for Koa","archived":false,"fork":false,"pushed_at":"2019-05-29T10:19:53.000Z","size":50,"stargazers_count":121,"open_issues_count":3,"forks_count":12,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-29T05:28:07.517Z","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/koajs.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","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":"2013-11-15T07:32:32.000Z","updated_at":"2024-10-19T14:09:43.000Z","dependencies_parsed_at":"2022-09-26T18:11:47.031Z","dependency_job_id":null,"html_url":"https://github.com/koajs/trie-router","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Ftrie-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Ftrie-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Ftrie-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Ftrie-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/trie-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252771779,"owners_count":21801780,"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-02T05:00:41.319Z","updated_at":"2025-05-06T21:30:05.562Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Middleware","仓库"],"sub_categories":["中间件"],"readme":"# Koa Trie Router\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n[![Gittip][gittip-image]][gittip-url]\n\n## About\n\n[Trie](http://en.wikipedia.org/wiki/Trie) routing for Koa based on [routington](https://github.com/jonathanong/routington).\n\nRoutes are orthogonal and strict, so the order of definition doesn't matter.  \nUnlike regexp routing, there's no wildcard routing and you can't `next` to the next matching route.\n\nSee [routington](https://github.com/jonathanong/routington) for more details.\n\n## Versions\n\n+ **Koa@1** is compatible with `1.x.x` versions of Trie-router\n+ **Koa@2** is compatible with `2.x.x` versions\n\n## Features\n\n+ Express-style routing using `router.get`, `router.put`, `router.post`, etc\n+ Named URL parameters\n+ Responds to `OPTIONS` requests with allowed methods\n+ Multiple route middleware\n+ Multiple routers\n+ Nestable routers\n+ `405 Method Not Allowed` support\n+ `501 Not Implemented` support\n\n## Notes\n\nThe router handles `/foo` and `/foo/` as the different urls (see why [one](https://github.com/koajs/trie-router/issues/13), [two](https://github.com/pillarjs/routington/issues/13)). If you need the same behavior for these urls just add [koa-no-trailing-slash](https://github.com/tssm/koa-no-trailing-slash) on the top of your middleware queue.\n\n## Usage\n\n```js\nconst Koa = require('koa')\nconst Router = require('koa-trie-router')\n\nlet app = new Koa()\nlet router = new Router()\n\nrouter\n  .use(function(ctx, next) {\n    console.log('* requests')\n    return next()\n  })\n  .get(function(ctx, next) {\n    console.log('GET requests')\n    return next()\n  })\n  .put('/foo', function (ctx) {\n    ctx.body = 'PUT /foo requests'\n  })\n  .post('/bar', function (ctx) {\n    ctx.body = 'POST /bar requests'\n  })\n\napp.use(router.middleware())\napp.listen(3000)\n```\n\n## API\n\n### router.use(middleware...)\nHandles all requests\n```js\nrouter.use(function(ctx) {\n  ctx.body = 'test' // All requests\n})\n```\n\n### router\\[method\\](middleware...)\nHandles requests only by one HTTP method\n```js\nrouter.get(function(ctx) {\n  ctx.body = 'GET' // GET requests\n})\n```\n\n### router\\[method\\]\\(paths, middleware...\\)\nHandles requests only by one HTTP method and one route\n\nWhere \n+ `paths` is `{String|Array\u003cString\u003e}`\n+ `middleware` is `{Function|Array\u003cFunction\u003e|AsyncFunction|Array\u003cAsyncFunction\u003e}`\n\nSignature\n```js\nrouter\n  .get('/one', middleware)\n  .post(['/two','/three'], middleware)\n  .put(['/four'], [middleware, middleware])\n  .del('/five', middleware, middleware, middleware)\n```\n\n### router.middleware()\n\nLike Express, all routes belong to a single middleware.\n  \nYou can use `koa-mount` for mounting of multiple routers:\n```js\nconst Koa = require('koa')\nconst mount = require('koa-mount')\nconst Router = require('koa-trie-router')\n\nlet app = new Koa()\nlet router1 = new Router()\nlet router2 = new Router()\n\nrouter1.get('/foo', middleware)\nrouter2.get('/bar', middleware)\n\napp.use(mount('/foo', router1.middleware()))\napp.use(mount('/bar', router2.middleware()))\n```\n\n### router.isImplementedMethod(method)\n\nChecks if the server implements a particular method and returns `true` or `false`.\nThis is not middleware, so you would have to use it in your own middleware.\n\n```js\napp.use(function(ctx, next) {\n  if (!router.isImplementedMethod(ctx.method)) {\n    ctx.status = 501\n    return\n  }\n  return next()\n})\n```\n\n### ctx.request.params\n`ctx.request.params` will be [defined](https://github.com/koajs/trie-router/blob/2.1.6/lib/Router.js#L176) with any matched parameters.\n\n```js\nrouter.get('/user/:name', async function (ctx, next) {\n  let name = ctx.request.params.name // or ctx.params.name\n  let user = await User.get(name)\n  return next()\n})\n```\n\n### Error handling\n\nThe middleware throws an error with `code` _MALFORMEDURL_ when it encounters\na malformed path. An application can _try/catch_ this upstream, identify the error\nby its code, and handle it however the developer chooses in the context of the\napplication- for example, re-throw as a 404.\n\n### Path Definitions\n\nFor path definitions, see [routington](https://github.com/jonathanong/routington).\n\n\n[npm-image]: https://img.shields.io/npm/v/koa-trie-router.svg?style=flat\n[npm-url]: https://npmjs.org/package/koa-trie-router\n[travis-image]: https://img.shields.io/travis/koajs/trie-router.svg?style=flat\n[travis-url]: https://travis-ci.org/koajs/trie-router\n[coveralls-image]: https://img.shields.io/coveralls/koajs/trie-router.svg?style=flat\n[coveralls-url]: https://coveralls.io/r/koajs/trie-router?branch=master\n[gittip-image]: https://img.shields.io/gittip/jonathanong.svg?style=flat\n[gittip-url]: https://www.gittip.com/jonathanong/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Ftrie-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Ftrie-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Ftrie-router/lists"}