🚅 Custom Router

Build a custom express-like router

Custom Deno Express-like Router

file_type_typescript router.ts
import { type Route, route, Handler } from "jsr:@std/http"

export class Router {
    #routes: Route[] = [];

    get(path: string, handler: Handler) {
        this.#addRoute("GET", path, handler);
    }

    post(path: string, handler: Handler) {
        this.#addRoute("POST", path, handler);
    }

    put(path: string, handler: Handler) {
        this.#addRoute("PUT", path, handler);
    }

    delete(path: string, handler: Handler) {
        this.#addRoute("DELETE", path, handler);
    }

    #addRoute(method: string, path: string, handler: Handler) {
        const pattern = new URLPattern({ pathname: path });
        this.#routes.push({
            pattern,
            method,
            handler: async (req, info, params) => {
                try {
                    return await handler(req, info!, params!);
                } catch (error) {
                    console.error("Error handling request:", error);
                    return new Response("Internal Server Error", { status: 500 });
                }
            },
        });
    }

    get handler() {
        return route(this.#routes, () => new Response("Not Found", { status: 404 }))
    }

}

Using the Custom Router

file_type_typescript main.ts
import { Router } from "./router.ts";
const app = new Router();

app.get('/', () => new Response('Hi Mom!'))

app.post('/health-check', () => new Response("It's ALIVE!"))

export default {
  fetch(req) {
    return app.handler(req);
  },
} satisfies Deno.ServeDefaultExport;

Questions? Let's chat

Open Discord