Skip to main content
Module

x/keywork/mod.ts>Router.KeyworkRouter#use

A library for building V8 Isolate web apps on Cloudflare Workers, Deno, and Node.JS
Go to Latest
method Router.KeyworkRouter.prototype.use
import { Router } from "https://deno.land/x/keywork@v6.0.1/mod.ts";
const { KeyworkRouter } = Router;

Combines additional routers and their respective route handlers to this router.

Route handlers are matched in the order of their declaration:

const app = new KeyworkRouter()

app.get('/foo', ({request}) => {
  return new Response('This handler got here first!')
})

app.get('/foo', ({request}) => {
  return new Response('This handler won't be called!')
})

However, if you want another router to act as middleware, Call use before defining your route handlers:

const authenticationRouter = new KeyworkRouter()

authenticationRouter.all('*', ({request, next}) => {
  if (!hasAuthCookie(request)) {
    return new KeyworkResourceError(401, "You need to be signed in to do that!")
  }

  // Pass the request along to the next matching route handler.
  return next()
})

const app = new KeyworkRouter()

app.use('/', authenticationRouter)

app.get('/user/profile', ({request}) => {
  return new Response("Some user only content.")
})

Parameters

fetcher: Fetcher<any>

Parameters

mountURLPattern: URLPatternLike | string
fetcher: Fetcher<any>