{"id":16254295,"url":"https://github.com/celer/keen-router","last_synced_at":"2025-07-17T19:38:45.674Z","repository":{"id":15197614,"uuid":"17925826","full_name":"celer/keen-router","owner":"celer","description":"A tree based, disambiguating, path router for web frameworks","archived":false,"fork":false,"pushed_at":"2014-11-05T05:16:41.000Z","size":213,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T02:43:43.368Z","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/celer.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-03-20T00:42:58.000Z","updated_at":"2014-10-30T20:35:04.000Z","dependencies_parsed_at":"2022-08-25T11:20:21.097Z","dependency_job_id":null,"html_url":"https://github.com/celer/keen-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/celer%2Fkeen-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celer%2Fkeen-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celer%2Fkeen-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celer%2Fkeen-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/celer","download_url":"https://codeload.github.com/celer/keen-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247847611,"owners_count":21006100,"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-10T15:21:01.732Z","updated_at":"2025-04-08T13:14:54.517Z","avatar_url":"https://github.com/celer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Keen Router\n\n[![Build Status](https://travis-ci.org/celer/keen-router.png)](https://travis-ci.org/celer/keen-router)\n[![Depdendency Status](https://david-dm.org/celer/keen-router.png)](https://david-dm.org/celer/keen-router)\n[![Code Coverage](https://coveralls.io/repos/celer/keen-router/badge.png?branch=master)](https://coveralls.io/r/celer/keen-router)\n\n## Introduction\n\nThis is a path router for use with your favorite web framework. \n\nIt is a bit special because:\n  \n  * It uses a tree based structure to store routes, it doesn't use a list like most popular routers\n    * It will use as few comparisons as possible to determine if a path is matched\n  * It can disambiguate between multiple conflicting routes, and will choose the best one\n\n## Routing logic\n\nRoutes are stored in a tree structure, so the moment a path element doesn't match we\nstop comparing routes. So let's imagine the following routes, in our router:\n\n  * /foo/bar\n  * /foo/:param\n  * /foo/:param/bar\n  * /foo/:param/baz\n\nA tree structure like so is created in the router:\n\n  * foo\n    * bar\n    * :param\n      * bar\n      * baz\n  \nIt uses a depth first search upon a tree to match routes, branching the search when \nmultiple possible routes are encountered. It should perform better then a router \nwhich uses a linear search method, especially when there are many branches in the\nroute. But the primary value isn't speed, it is the ability to disambiguate between\nmultiple conflicting routes.  \n\nThe router will prefer exact path element matches over parameter matches, but \nwill still consider the parameter matches. \n\nSo in the case where we would have multiple possible matches:\n \n  * /a/:1/:3/:4 \n  * /:1/b/:2/:3\n  * /:1/:2/c/:3\n  * /:1/:2/:3/d\n\nwhere a path like:\n  \n  /a/b/c/d\n\nCould match any of the above paths, the router will choose the route which \nwas added first.  \n\n## Examples\n\nHere is a very simplistic example, where we define multiple routes\n\n```javascript\n  var Router = require('keen-router');\n\n  var r = new Router();\n\n  //We'll attach some arbitrary data to this route\n  r.add(\"/user\", { call: \"User.create\" });\n  r.add(\"/account\");\n  r.add(\"/mailbox\");\n  r.add(\"/user/:id\");\n\n  r.resolve(\"/user\"); // returns { route:\"/user\", params:{}, data: { call: \"User.create\"}});\n  r.resolve(\"/user/55\"); // returns { route:\"/user/:id\", params:{id:\"55\"}});\n\n  //Later we could decide to remove a route:\n  r.remove(\"/user\");\n\n  r.list(); //returns [\"/account\",\"/mailbox\",\"/user/:id\"]\n  \n```\n\nHere is an example which uses call backs:\n\n```javascript\n  var Router = require('keen-router');\n\n  var r = new Router();\n  \n  // the :bar is a parameter and will be put into the params hash\n  r.add(\"/foo/:bar\",function(route,params){\n    //route would contain the route if matched, i.e. /foo/:bar\n    //params would contain the matched parameters, i.e. { bar:\"bar\"}\n  });\n\n  r.resolve(\"/foo/bar\");\n```  \n\nYou can also specify your own tokenizer for routes:\n\n```javascript\n  \n  var Router = require('keen-router');\n\n  var r = new Router(function(path){\n    return path.split(\"|\");\n  });\n\n```\n\nThis means that you can support routes in various formats such as:\n\n\u003e METHOD HOSTNAME PATH\n\nsuch as\n\n\u003e GET foo.com /user\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceler%2Fkeen-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceler%2Fkeen-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceler%2Fkeen-router/lists"}