Skip to content

How requests are handled

Routing, middleware, errors, per-request state and portability, in brief. Each package page has the detail.

Paths are literal segments, :name parameters and a trailing *. Literal segments win over parameters, which win over the wildcard, and the router backtracks when a branch has no route for the method. A trailing slash is ignored. c.params is typed from the path string. A path that matches under another method gets 405 with an Allow header; HEAD is served from the GET route and OPTIONS is answered automatically.

Routes and prefix middleware match the same normalised path, so percent-encoding a character cannot route around a guard. See Routing for the full rules.

use() applies to every request, or to requests under a literal prefix, in registration order. It also wraps 404 and 405 responses, so headers such as CORS appear on them.

Anything a handler or middleware throws becomes a response at the point it was thrown, then passes back out through the surrounding middleware. errorHandler() from @edgetime/http renders RFC 9457 problem details. HttpError messages reach the client; any other error becomes an opaque 500, and the original is kept on c.error for the access log. Pass body to errorHandler() to send another JSON shape with the same status and headers.

Nothing request-scoped is stored at module level. Values pass between middleware through c.set() and c.get(), typed by the app’s vars. Use provide() for values built on first use within a request, and perEnv() for clients and verifiers that are built from bindings and reused across requests.

router.fetch(request, env?, ctx?) needs only the Fetch API. Bindings are described by the methods used, so a Cloudflare binding, a Node implementation or a test double all fit: rateLimit() takes anything with limit({ key }), and cached() anything with match and put.