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
RequestRouter
define 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-regexp
style path patterns.Path matching is implemented via the JavaScript native
URLPattern
You may need a polyfill if your app uses on a runtime that hasn't yet added
URLPattern
class.Learn more from the URI Module ›
IsomorphicFetchEvent
When creating a
RouteRequestHandler
callback, you have access to anIsomorphicFetchEvent
:IsomorphicFetchEvent.request
The incoming request received by the V8 runtime.
IsomorphicFetchEvent<ExpectedParams>.params
Parameters 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.env
The bound environment aliases. Bound environment aliases are mostly limited to Cloudflare Workers, and are usually defined in your
wrangler.toml
file.Node.js
This is similar to
process.env
.IsomorphicFetchEvent.data
Optional extra data to be passed to a route handler, usually from middleware.
IsomorphicFetchEvent.originalURL
The original request URL, unmodified by Keywork's middleware logic.
Middleware
Similar to Express.js's concept of Middleware, route handlers have access to a
next
function to pass a request off to the next route handler matching the URL pattern.next
can also be called after checking for some criteria, such as if the user has authenticated:Overrides
Providing a
request
argument 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
RequestRouter
class 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
Response
for the return type of yourRouteHandler
, allowing you to skip the ceremony of constructingResponse
with the appropriate headersHowever, this behavior can be avoided by explicitly providing a
Response
object, or a class that extends fromResponse
such as...CachableResponse
HTMLResponse
JSONResponse
ErrorResponse
Errors
Errors in your code are caught before they crash the runtime. See
KeyworkResourceError
for further details.Sessions
Support for cookie-based sessions is automatically handled. See Keywork#Session.SessionMiddleware
SessionMiddleware
for further details.Related Entries
Further reading