route(routes: Route[],defaultHandler: (request: Request,info?: Deno.ServeHandlerInfo,) => Response | Promise<Response>,): (request: Request,info?: Deno.ServeHandlerInfo,) => Response | Promise<Response>
Routes requests to different handlers based on the request path and method.
▶Usage
Usage
import { route, type Route } from "@std/http/unstable-route"; import { serveDir } from "@std/http/file-server"; const routes: Route[] = [ { pattern: new URLPattern({ pathname: "/about" }), handler: () => new Response("About page"), }, { pattern: new URLPattern({ pathname: "/users/:id" }), handler: (_req, _info, params) => new Response(params?.pathname.groups.id), }, { pattern: new URLPattern({ pathname: "/static/*" }), handler: (req: Request) => serveDir(req) }, { method: ["GET", "HEAD"], pattern: new URLPattern({ pathname: "/api" }), handler: (req: Request) => new Response(req.method === 'HEAD' ? null : 'ok'), }, ]; function defaultHandler(_req: Request) { return new Response("Not found", { status: 404 }); } Deno.serve(route(routes, defaultHandler));
routes: Route[]
Route configurations
defaultHandler: (request: Request,info?: Deno.ServeHandlerInfo,) => Response | Promise<Response>
Default request handler that's returned when no route matches the given request. Serving HTTP 404 Not Found or 405 Method Not Allowed response can be done in this function.
(request: Request,info?: Deno.ServeHandlerInfo,) => Response | Promise<Response>
Request handler