Small opinionated router for Deno.
- Uses
URLPatternfor route matching which supports Express-like route params - Does not extend
Requestor have any helper functions - Strives to be very simple and very minimal
import { createRouter } from "https://deno.land/x/[email protected]/mod.ts";
import { serve } from "https://deno.land/[email protected]/http/mod.ts";
function getProject(req: Request, params: Record<"id", string>) {
return new Response(getProjectById(params.id));
}
const router = createRouter({
"/api/projects": {
"GET": getProjects,
"POST": createProject,
},
"/api/projects/:id": {
"GET": getProject,
},
"/api/check": check, // Will respond to any HTTP method
});
await serve(router);Goal is to have tiny wrapper around standard Request, Response and
URLPattern workflow.
It doesn't have middleware interface (e.g. router.get('/route', endpoint)) and
accepts mapping object instead.
Router does not extend default Request object and instead passes route params
as second argument to handler function.
This router written to be as fast as possible while using URLPattern (which
uses path-to-regexp), which is slower than, e.g. using Radix Trees or RegEx
for matching.
Benchmark script with comparison to several popular Deno routers is available.
Run it with deno bench --unstable.