Router <Context, Routes>
Hierarchy
- Router
Index
Methods
addDefaultHandler
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. Passoptionsto give the default route its ownrequestHandlerTimeoutSecs, overriding the crawler's default for requests that fall through to it.Parameters
handler: (ctx) => Awaitable<void>
options: RouteOptions = {}
Returns void
addHandler
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. Passoptionsto give this route its ownrequestHandlerTimeoutSecs, overriding the crawler's default for requests with this label.Parameters
label: Label
handler: (ctx) => Awaitable<void>
optionaloptions: RouteOptions
Returns void
getHandler
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>
getMaxTimeoutSecs
The longest
requestHandlerTimeoutSecsany route asked for, orundefinedwhen no route overrides it. The crawler needs an upper bound up front, before it knows which routes a run will actually hit.Returns undefined | number
getTimeoutSecs
Returns the
requestHandlerTimeoutSecsregistered for a label, orundefinedwhen the route did not override it and the crawler's own timeout should apply. Falls back to the default route the same waygetHandlerdoes, so a label with no route of its own inherits whatever the default route asked for. Used by the crawler; not meant to be called directly.Parameters
optionallabel: string | symbol
Returns undefined | number
use
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
staticcreate
Creates new router instance. This instance can then serve as a
requestHandlerof your crawler.import { Router, CheerioCrawler, CheerioCrawlingContext } from 'crawlee';const router = Router.create<CheerioCrawlingContext>();router.addHandler('label-a', async (ctx) => {ctx.log.info('...');});router.addDefaultHandler(async (ctx) => {ctx.log.info('...');});const crawler = new CheerioCrawler({requestHandler: router,});await crawler.run();Parameters
optionalroutes: RouterRoutes<Context, Routes>
Returns RouterHandler<Context, Routes>
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.A single route can take longer than the rest without raising the crawler-wide
requestHandlerTimeoutSecsfor everything - pass a per-route timeout as the last argument:When the time a route needs is only apparent once it is already running, call
context.extendTimeoutfrom inside the handler: