Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/http_server/lib/router.ts

🌐 Light-weight http server
Go to Latest
File
import { Middleware, MiddlewareHandler } from './middleware.ts';
export class Router { #handlers = new Set<Middleware>(); #base: string;
/** * Create a new router. * * @param base - The base route. */ constructor(base = '') { this.#base = base; }
/** * Listen for incoming requests. * @param route - The route to listen on. * @param method - The method to listen for. * @example * server.on()(({ respond }) => respond('Hello World')); * server.on('/')(({ respond }) => respond('Hello World')); * server.on('/', 'GET')(({ respond }) => respond('Hello World')); * * @returns A function that can later be used to add handlers. */ on(route = '/*', method = 'GET'): (...handlers: MiddlewareHandler[]) => Router { /** * A function to add handlers. * * @param handlers - The handlers to add. * @example * server.on()(({ respond }) => respond('Hello World')); * server.on('/')(({ respond }) => respond('Hello World')); * server.on('/', 'GET')(({ respond }) => respond('Hello World')); */ return (...handlers: MiddlewareHandler[]) => { route = `${this.#base}/${route}`; handlers.forEach(handler => this.#handlers.add(Middleware(method, route, handler)));
return this; }; }
/** * Get router handlers. * * @returns Router handlers. */ handlers(): Middleware[] { return [...this.#handlers]; }}