Package Exports
- @hattip/router
Readme
@hattip/router
Express-style imperative router for HatTip.
Usage
import { compose } from "@hattip/compose";
import { createRouter } from "@hattip/router";
const router = createRouter();
router.get("/", () => new Response("Hello, world!"));
router.post("/echo", async (ctx) => new Response(await ctx.request.text()));
router.delete(
"/book/:title",
async (ctx) => new Response(`Deleted book: ${ctx.params.title}`),
);
export default compose(router.handlers);API
createRouter(): Creates a new router.router.<method>(path, handler): Adds a handler for the given method and path.pathcan be a string or a regular expression. If it's a string, parameters can be specified using:followed by the parameter name like/book/:title.*can be used to match any number of characters.<method>is the HTTP method in lowercase (e.g.get,post,delete).handleris a request handler.router.all(path, handler): Adds a handler for all methods and the given path.router.handlers: Returns an array of handlers that can be passed to thecomposefunction.
@hattip/router extends the RequestContext object with the following properties:
url: AURLobject representing the current URL. You can access, e.g., query parameters withurl.searchParams.method: The HTTP method of the request ("GET","POST", etc.).params: A map of parameters extracted from the path.
We recommend the use of context.url and context.method for URL rewriting and method overrides instead of context.request.url and context.request.method which are immutable.