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). When the router was created with a defaultRoute schema,
request.userDatais typed from it; otherwise it defaults to the context's (loosely typed)userData. 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:Passing a Standard Schema per label instead of a plain type both infers the
request.userDatatypes and validates them at runtime — when the request is handled, and when it is added to the crawler (crawler.addRequests,context.addRequests,enqueueLinks). A failing request throws a RequestValidationError.