Skip to main content
Module

x/workers_router/index.ts>WorkerRouter#use

A router for Worker Runtimes such and Cloudflare Workers or Service Workers.
Latest
method WorkerRouter.prototype.use
Deprecated
Deprecated

The name of this method might change to avoid confusion with use method known from other routers.

import { WorkerRouter } from "https://deno.land/x/workers_router@v0.3.0-pre.6/index.ts";

Use a different WorkerRouter for the provided pattern. Keep in mind that:

  • The pattern must end in a wildcard *
  • The corresponding match is the only part used for matching in the subRouter
  • Forwards all HTTP methods
  • Does not apply any middleware

Why does it not apply middleware?

There are 2 reasons: First, it interferes with type inference of middleware. As a developer you'd have to provide the correct types at the point of defining the sub router, which is at least as cumbersome as providing the middleware itself.

Second, without this there would be no way to opt a route out of the router-level middleware. For example you might want to opt out all /public* urls from cookie parsing, authentication, etc. but add a different caching policy instead.

Parameters

path: string

A pattern ending in a wildcard, e.g. /items*

subRouter: WorkerRouter<Y>

A WorkerRouter that handles the remaining part of the URL, e.g. /:category/:id