Skip to main content

Middleware

Module Overview

Middleware are instances of the RequestRouter, each with single responsibility e.g. (users, blog posts, payments, authentication, etc.)

import { UsersRouter } from '@local/worker/routers/UsersRouter'
import { BlogRouter } from '@local/worker/routers/BlogRouter'
import { AuthenticationRouter } from '@local/worker/routers/AuthenticationRouter'
import { CloudflarePagesAssetRouter } from 'keywork/assets'
import { RequestRouter } from 'keywork/router'

// Create a router to receive all incoming requests...
const app = new RequestRouter({
// Here we combine our routers...
middleware: [
AuthenticationRouter,
UsersRouter,
BlogRouter,
// And serve static assets...
new CloudflarePagesAssetRouter(),
],
})

Middleware routers can perform any task that of single router such as...

  • Executing any code
  • Make changes to the request and the response of another router
  • Terminate a request
  • Intercept a request to check for authentication
  • Call the next route handler in the stack

Built-in Middleware

Keywork includes a number of built-in middleware for common use cases:

Usage

import * as Middleware from 'keywork/middleware'

Asset Router Classes

Cloudflare Middleware Classes

Other Classes

Cloudflare Middleware Interfaces

Other Interfaces

Request Interfaces

Session Management Interfaces