Skip to content

A very simple radix tree and rotuer based on radix/patrica trees made for deno

Notifications You must be signed in to change notification settings

brecert/radix-router-deno

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Radix Rotuer Deno

A very simple radix tree and rotuer based on radix/patrica 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

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<RouteHandler>({
  "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" });
  }
}

About

A very simple radix tree and rotuer based on radix/patrica trees made for deno

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published