{"id":13736865,"url":"https://github.com/zhmushan/router","last_synced_at":"2025-04-30T10:44:58.769Z","repository":{"id":40811517,"uuid":"264541917","full_name":"zhmushan/router","owner":"zhmushan","description":"A high-performance basic router works anywhere.","archived":false,"fork":false,"pushed_at":"2022-06-23T04:33:26.000Z","size":60,"stargazers_count":55,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-07T17:39:02.330Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/zhmushan.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":"2020-05-16T22:49:00.000Z","updated_at":"2023-09-08T08:21:18.000Z","dependencies_parsed_at":"2022-09-14T01:41:58.031Z","dependency_job_id":null,"html_url":"https://github.com/zhmushan/router","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhmushan%2Frouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhmushan%2Frouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhmushan%2Frouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zhmushan%2Frouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zhmushan","download_url":"https://codeload.github.com/zhmushan/router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242640885,"owners_count":20162051,"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-03T03:01:29.983Z","updated_at":"2025-03-09T03:31:38.760Z","avatar_url":"https://github.com/zhmushan.png","language":"TypeScript","funding_links":[],"categories":["基础设施","Modules"],"sub_categories":["JAM Stack/静态站点","Online Playgrounds","Assistants","Web utils"],"readme":"# Router\n\nA high-performance basic router works anywhere.\n\n[![tag](https://img.shields.io/github/tag/zhmushan/router.svg)](https://github.com/zhmushan/router)\n[![Build Status](https://github.com/zhmushan/router/workflows/ci/badge.svg?branch=master)](https://github.com/zhmushan/router/actions)\n[![license](https://img.shields.io/github/license/zhmushan/router.svg)](https://github.com/zhmushan/router)\n\n## Features\n\n- **Based on [radix tree](https://en.wikipedia.org/wiki/Radix_tree)**: Compared\n  with routers based on regular expressions, we have better performance in most\n  of the cases, which can significantly increase the speed of your project, and\n  as the project scale increases, the performance will also increase\n  exponentially.\n\n- **Stupid rules**: We will always match according to the rules of \"Static \u003e\n  Param \u003e Any\". For \"static routes\", we always match strictly equal strings. For\n  \"param routes\", we will match 1 or more characters, ending with \"/\". For \"any\n  routes\", we will match 0 or more characters.\n\n## Usage\n\n- [Deno](#deno)\n- [Nodejs](#nodejs)\n- [Browser](#browser)\n\n### Deno\n\nSee [zhmushan/abc](https://github.com/zhmushan/abc)\n\n### Nodejs\n\nInstallation:\n\n```\nnpm i zhmushan/router#v2\n```\n\nCreate `index.js`:\n\n```js\n// import * as http from \"http\";\n// import { Node } from \"router\";\n\nconst http = require(\"http\");\nconst Node = require(\"router\").Node;\n\nconst root = new Node();\n\nroot.add(\"/:user\", (p) =\u003e {\n  return p.get(\"user\");\n});\n\nhttp.createServer((req, res) =\u003e {\n  const [h, p] = root.find(req.url);\n\n  if (h) {\n    const result = h(p);\n    res.end(result);\n  } else {\n    res.end(\"Not Found\");\n  }\n}).listen(8080);\n\nconsole.log(\"server listening on http://localhost:8080\");\n```\n\nRun:\n\n```\nnode index.js\n```\n\nBrowse to http://localhost:8080/your_name and you should see \"your_name\" on the\npage.\n\n### Browser\n\n```html\n\u003cbody\u003e\n  \u003cbutton id=\"change_path\"\u003eChange Path\u003c/button\u003e\n  \u003cbutton id=\"home\"\u003eHome\u003c/button\u003e\n  \u003cscript type=\"module\"\u003e\n    import { Node } from \"https://deno.land/x/router@v2.0.1/mod.js\";\n\n    const root = new Node();\n    root.add(\"/:random_string\", (c) =\u003e {\n      console.log(c.get(\"random_string\"));\n    });\n\n    change_path.onclick = () =\u003e {\n      const path = `/${randomStr()}`;\n      const [func, params] = root.find(path);\n      if (func) {\n        func(params);\n        history.replaceState(undefined, \"\", path);\n      }\n    };\n\n    home.onclick = () =\u003e {\n      history.replaceState(undefined, \"\", \"/\");\n    };\n\n    function randomStr() {\n      return Math.random().toString(32).split(\".\")[1];\n    }\n  \u003c/script\u003e\n\u003c/body\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhmushan%2Frouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzhmushan%2Frouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzhmushan%2Frouter/lists"}