Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

x/keywork/router/classes/mod.ts>KeyworkRouter#use

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

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

Route handlers are matched in the order of their declaration:

const app = new RequestRouter()

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 RequestRouter()

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 RequestRouter()

app.use('/', authenticationRouter)

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

Parameters

fetcher: FetcherLike<BoundAliases>

Parameters

mountURLPattern: URLPatternLike
fetcher: FetcherLike<BoundAliases>