Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhmushan/router
A high-performance basic router works anywhere.
https://github.com/zhmushan/router
Last synced: 17 days ago
JSON representation
A high-performance basic router works anywhere.
- Host: GitHub
- URL: https://github.com/zhmushan/router
- Owner: zhmushan
- License: mit
- Created: 2020-05-16T22:49:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-06-23T04:33:26.000Z (over 2 years ago)
- Last Synced: 2024-10-10T12:17:31.654Z (29 days ago)
- Language: TypeScript
- Homepage:
- Size: 58.6 KB
- Stars: 55
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-deno-cn - @zhmushan/router
- awesome-deno - router - A high-performance basic router works anywhere. (Modules / Web utils)
README
# Router
A high-performance basic router works anywhere.
[![tag](https://img.shields.io/github/tag/zhmushan/router.svg)](https://github.com/zhmushan/router)
[![Build Status](https://github.com/zhmushan/router/workflows/ci/badge.svg?branch=master)](https://github.com/zhmushan/router/actions)
[![license](https://img.shields.io/github/license/zhmushan/router.svg)](https://github.com/zhmushan/router)## Features
- **Based on [radix tree](https://en.wikipedia.org/wiki/Radix_tree)**: Compared
with routers based on regular expressions, we have better performance in most
of the cases, which can significantly increase the speed of your project, and
as the project scale increases, the performance will also increase
exponentially.- **Stupid rules**: We will always match according to the rules of "Static >
Param > Any". For "static routes", we always match strictly equal strings. For
"param routes", we will match 1 or more characters, ending with "/". For "any
routes", we will match 0 or more characters.## Usage
- [Deno](#deno)
- [Nodejs](#nodejs)
- [Browser](#browser)### Deno
See [zhmushan/abc](https://github.com/zhmushan/abc)
### Nodejs
Installation:
```
npm i zhmushan/router#v2
```Create `index.js`:
```js
// import * as http from "http";
// import { Node } from "router";const http = require("http");
const Node = require("router").Node;const root = new Node();
root.add("/:user", (p) => {
return p.get("user");
});http.createServer((req, res) => {
const [h, p] = root.find(req.url);if (h) {
const result = h(p);
res.end(result);
} else {
res.end("Not Found");
}
}).listen(8080);console.log("server listening on http://localhost:8080");
```Run:
```
node index.js
```Browse to http://localhost:8080/your_name and you should see "your_name" on the
page.### Browser
```html
Change Path
Home
import { Node } from "https://deno.land/x/[email protected]/mod.js";const root = new Node();
root.add("/:random_string", (c) => {
console.log(c.get("random_string"));
});change_path.onclick = () => {
const path = `/${randomStr()}`;
const [func, params] = root.find(path);
if (func) {
func(params);
history.replaceState(undefined, "", path);
}
};home.onclick = () => {
history.replaceState(undefined, "", "/");
};function randomStr() {
return Math.random().toString(32).split(".")[1];
}
```