@std/net/router.ms#
Import each required symbol with import Name from "@std/net/router.ms".
Route Type#
A registered route: the method token and pattern it answers, and the handler that runs on a match.
type Route {
method String
pattern String
segments List[Segment]
around List[Int]
handler fn(var HttpExchange, RouteParams) Result[Void, Error]
}Route.around#
The middleware groups of the routers this route was mounted through, outermost
first, as positions in Router.groups.
around List[Int]Route.handler#
Runs when the route matches, with the values the pattern captured.
handler fn(var HttpExchange, RouteParams) Result[Void, Error]Route.method#
The method token the route answers, such as GET.
method StringRoute.pattern#
The pattern text as registered.
pattern StringRoute.segments#
The pattern split at / into segments, in path order.
segments List[Segment]RouteParams Type#
The values a matched pattern captured, by name.
type RouteParams {
names List[String]
values List[String]
}RouteParams.names#
Names in pattern order.
names List[String]RouteParams.values#
The captured text for each name; a wildcard holds the remaining segments joined
by /.
values List[String]RouteParams.get#
Returns the text captured for name as a view, or null when the pattern has no such
parameter.
fn get(name String) String?RouteParams.get_or#
Returns the text captured for name, or fallback when the pattern has no such
parameter.
fn get_or(name String, fallback String) StringRouter Type#
Routes requests by method and path and wraps them in middleware.
Patterns are / followed by literal segments, :name parameters that match one
segment, and a final *name that matches the rest of the path (possibly nothing).
The first route registered that matches wins, and a trailing slash is part of the
path.
type Router {
routes List[Route] = []
middleware List[fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error]] = []
groups List[List[fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error]]] = []
}Router.groups#
The middleware of each mounted router, in mount order; a route names the groups around it.
groups List[List[fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error]]] = []Router.middleware#
Middleware in the order it wraps requests: the first one added runs outermost.
middleware List[fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error]] = []Router.routes#
Registered routes in registration order.
routes List[Route] = []Router.call#
Runs request through the router without a socket and returns the response it
produced. A handler error is returned as is; the server would answer it with a
500. A handler that sends nothing answers 204.
fn call(request HttpRequest) Result[HttpResponse, Error]Router.delete#
Registers handler for DELETE requests to pattern; shorthand for
route(HttpMethod.Delete, pattern, handler).
var fn delete(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.get#
Registers handler for GET requests to pattern; shorthand for
route(HttpMethod.Get, pattern, handler).
var fn get(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.handler#
Returns the function to pass to serve; it takes the router, so register every route
first. Unmatched paths answer 404. A path matched only under other methods answers
405 with Allow, and OPTIONS on a known path answers 204 with Allow. OPTIONS *
answers 204 with the Allow of every registered method.
move fn handler() fn(var HttpExchange) Result[Void, Error]Router.head#
Registers handler for HEAD requests to pattern; shorthand for
route(HttpMethod.Head, pattern, handler).
var fn head(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.mount#
Moves every route of routes under prefix, wrapped in that router’s own
middleware. The prefix is / followed by segments with no trailing slash, and it
may hold parameters. The mounted router’s / route answers prefix itself. A
router holds functions, which have one owner, so routes is taken: build it
again to mount it elsewhere. An invalid prefix returns EINVAL, and a route that
duplicates an existing method and pattern shape returns EEXIST.
var fn mount(prefix String, move routes Router) Result[Void, Error]Router.options#
Registers handler for OPTIONS requests to pattern; shorthand for
route(HttpMethod.Options, pattern, handler).
var fn options(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.patch#
Registers handler for PATCH requests to pattern; shorthand for
route(HttpMethod.Patch, pattern, handler).
var fn patch(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.post#
Registers handler for POST requests to pattern; shorthand for
route(HttpMethod.Post, pattern, handler).
var fn post(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.put#
Registers handler for PUT requests to pattern; shorthand for
route(HttpMethod.Put, pattern, handler).
var fn put(pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.route#
Registers handler for method and pattern. A route also answers HEAD when
method is GET and no HEAD route matches. An invalid pattern returns EINVAL, and
registering the same method and pattern shape twice returns EEXIST.
var fn route(method HttpMethod, pattern String, move handler fn(var HttpExchange, RouteParams) Result[Void, Error]) Result[Void, Error]Router.use#
Adds middleware around every route and around the 404, 405 and OPTIONS answers.
It receives the exchange and a continuation that runs the rest of the chain with
the exchange it is given, so a middleware can pass on a wrapped one (see
request_id and access_log).
var fn use(move middleware fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error]) VoidSegment Enum#
One segment of a route pattern.
enum Segment {
Literal(text String)
Param(name String)
Rest(name String)
}Segment.Literal#
Matches exactly this text.
Literal(text String)Segment.Param#
Matches one nonempty segment and captures it.
Param(name String)Segment.Rest#
Matches the rest of the path, possibly nothing, and captures it joined by /.
Rest(name String)Segment.Literal.text#
The exact text a request path segment must equal, case-sensitively.
text StringSegment.Param.name#
The name RouteParams.get finds the captured segment under.
name StringSegment.Rest.name#
The name RouteParams.get finds the remaining path under.
name Stringaccess_log Function#
Middleware that writes one Info record named request to logger after each
request, with the method, path, protocol, response status, body bytes written and
duration in milliseconds. A handler that sends nothing is logged as 204 and one that
fails before sending as 500. A logging failure never fails the request.
fn access_log(move logger Logger) fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error] { ... }request_id Function#
Middleware that gives every request an id: the request’s own X-Request-Id when it
is 1 to 128 visible ASCII characters (no spaces), otherwise a new UUID. The id is set
on the request headers the wrapped exchange reports, where handlers and later
middleware read it, and added to the response as X-Request-Id.
fn request_id() fn(var HttpExchange, fn(var HttpExchange) Result[Void, Error]) Result[Void, Error] { ... }with_request_headers Function#
Replaces exchange with one whose headers() reports request and everything else
passes through. Middleware uses it to hand later layers changed request headers,
since a received header set is a copy.
fn with_request_headers(var exchange HttpExchange, move request HttpHeaders) Void { ... }