Router
Module Overview
Usage
- Node
- Deno
- Browser/ESM
import * as Router from 'keywork/router'
import * as Router from 'https://deno.land/x/keywork/router'
let Router = await import('https://esm.sh/keywork/router')
Router Classes
Other Interfaces
Request Interfaces
Request Handler Interfaces
Options Type Aliases
Other Type Aliases
Request Handler Functions
Exports
KeyworkRouter
Renames and re-exports RequestRouter
RequestRouterOptions
Re-exports RequestRouterOptions
Designed with familiarity in mind, the server-side routing API is inspired by Express.js, React Router, and the native Cloudflare Workers platform.
Creating a RESTful API
Instances of
RequestRouterdefine each route handler by invoking methods that correspond with HTTP method of the same name:'GET'app.get([path pattern], [RouteRequestHandler])'POST'app.post([path pattern], [RouteRequestHandler])'PATCH'app.patch([path pattern], [RouteRequestHandler])'DELETE'app.delete([path pattern], [RouteRequestHandler])'HEAD'app.head([path pattern], [RouteRequestHandler])'OPTIONS'app.options([path pattern], [RouteRequestHandler])'*'app.all([path pattern], [RouteRequestHandler])GET(app.get([path pattern], [...RouteRequestHandler]))POST(app.post([path pattern], [...RouteRequestHandler]))Path Parameters
Routes are defined with a
path-to-regexpstyle path patterns.Path matching is implemented via the JavaScript native
URLPatternYou may need a polyfill if your app uses on a runtime that hasn't yet added
URLPatternclass.Learn more from the URI Module ›
IsomorphicFetchEventWhen creating a
RouteRequestHandlercallback, you have access to anIsomorphicFetchEvent:IsomorphicFetchEvent.requestThe incoming request received by the V8 runtime.
IsomorphicFetchEvent<ExpectedParams>.paramsParameters parsed from the incoming request's URL and the route's pattern.
This can be made type-safe by providing a generic type when defining the route:
IsomorphicFetchEvent.envThe bound environment aliases. Bound environment aliases are mostly limited to Cloudflare Workers, and are usually defined in your
wrangler.tomlfile.Node.js
This is similar to
process.env.IsomorphicFetchEvent.dataOptional extra data to be passed to a route handler, usually from middleware.
IsomorphicFetchEvent.originalURLThe original request URL, unmodified by Keywork's middleware logic.
Middleware
Similar to Express.js's concept of Middleware, route handlers have access to a
nextfunction to pass a request off to the next route handler matching the URL pattern.nextcan also be called after checking for some criteria, such as if the user has authenticated:Overrides
Providing a
requestargument will override the path param parsing withinRequestRouter. This can be useful if your middleware needs to modify or omit some request information before reaching the next route handler.Additional Perks
The
RequestRouterclass also provides some small quality-of-life improvements over the low-level APIs of the Workers platform.Automatic Response Parsing
Keywork will automatically infer the appropriate
Responsefor the return type of yourRouteHandler, allowing you to skip the ceremony of constructingResponsewith the appropriate headersHowever, this behavior can be avoided by explicitly providing a
Responseobject, or a class that extends fromResponsesuch as...CachableResponseHTMLResponseJSONResponseErrorResponseErrors
Errors in your code are caught before they crash the runtime. See
KeyworkResourceErrorfor further details.Sessions
Support for cookie-based sessions is automatically handled. See Keywork#Session.SessionMiddleware
SessionMiddlewarefor further details.Related Entries
Further reading