https://github.com/brecert/radix-router-deno
A very simple radix tree and rotuer based on radix/patrica trees made for deno
https://github.com/brecert/radix-router-deno
deno radix-tree router typescript
Last synced: 2 months ago
JSON representation
A very simple radix tree and rotuer based on radix/patrica trees made for deno
- Host: GitHub
- URL: https://github.com/brecert/radix-router-deno
- Owner: brecert
- Created: 2021-03-10T06:14:15.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-03-10T06:20:38.000Z (over 5 years ago)
- Last Synced: 2025-07-03T22:04:04.972Z (12 months ago)
- Topics: deno, radix-tree, router, typescript
- Language: TypeScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Radix Rotuer Deno
> A very simple [radix tree](radix_trees) and rotuer based on [radix/patrica trees](radix_trees) made for deno
Please note that performance hasn't actually been tested.
This isn't particuarly useful unless you have completely static routes really, there's no wildcards or any sort of advanced string matching, although I do hope to add those eventually.
# Example
```ts
import {
Response,
serve,
ServerRequest,
} from "https://deno.land/std@0.90.0/http/server.ts";
import { Router } from "../router.ts";
type RouteHandler = (ctx: ServerRequest) => void;
const routes = new Router({
"GET /": (ctx) => {
ctx.respond({ body: "hello world!" });
},
"GET /about": (ctx) => {
ctx.respond({ body: JSON.stringify({ version: "0.0.0" }) });
},
"POST /ping": (ctx) => {
ctx.respond({ body: "pong!" });
},
});
const server = serve({ port: 8080 });
console.log("listening on localhost:8080");
for await (const req of server) {
const path = `${req.method} ${req.url}`;
const fn = routes.getData(path);
if (fn) {
fn(req);
} else {
req.respond({ status: 404, body: "404 not found" });
}
}
```
[radix_trees]: (https://wikipedia.org/wiki/Radix_tree)