RouterHandler <Context, Routes>
Hierarchy
- Router<Context, Routes>
- RouterHandler
Callable
Parameters
ctx: Context
Returns Awaitable<void>
Index
Methods
inheritedaddDefaultHandler
Registers default route handler. As a fallback it can receive any request (including labels not declared in the route map), so
request.userDatadefaults to the context'suserDatatype (loosely typed by default). Pass an explicitUserDatatype argument to narrow it.Parameters
handler: (ctx) => Awaitable<void>
Returns void
inheritedaddHandler
Registers new route handler for given label. When the router declares a route map, the
labelis restricted to the declared labels andrequest.userDatais typed accordingly.Parameters
label: Label
handler: (ctx) => Awaitable<void>
Returns void
inheritedgetHandler
Returns route handler for given label. If no label is provided, the default request handler will be returned.
Parameters
optionallabel: string | symbol
Returns (ctx) => Awaitable<void>
Parameters
ctx: Context
Returns Awaitable<void>
inheriteduse
Registers a middleware that will be fired before the matching route handler. Multiple middlewares can be registered, they will be fired in the same order.
Parameters
middleware: (ctx) => Awaitable<void>
Returns void
Simple router that works based on request labels. This instance can then serve as a
requestHandlerof your crawler.Alternatively we can use the default router instance from crawler object:
For convenience, we can also define the routes right when creating the router:
Middlewares are also supported via the
router.usemethod. There can be multiple middlewares for a single router, they will be executed sequentially in the same order as they were registered.To get
request.userDatatyped per label, declare a route map and pass it as the second type argument. The label passed to Router.addHandler then drives the type ofrequest.userData, and unknown labels are rejected at compile time: