Options
All
  • Public
  • Public/Protected
  • All
Menu

A router.

Hierarchy

  • Router

Index

Constructors

constructor

  • Parameters

    • Default value options: RouterOptions = { excludeBuiltInRoutes: false }

    Returns Router

Properties

preloadRequests

preloadRequests: PreloadRequests = new PreloadRequests()

routeGroups

routeGroups: RouteGroupList = new RouteGroupList()

Methods

catch

  • catch(error: RegExp | string | number, handler: RouteHandler): this
  • Adds a route that matches all requests with internal error header.

    Parameters

    • error: RegExp | string | number
    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

delete

  • Adds a route matching DELETE HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

destination

  • destination(name: string, router: Router): this
  • Adds a named destination to which you can map traffic using the traffic shaping settings in your environment in Layer0 developer console

    Parameters

    • name: string

      The name of the destination

    • router: Router

      A router to use when handling requests

    Returns this

    A self-reference, suitable for chaining.

fallback

  • Adds a route that will run if no other routes are matched

    Parameters

    • handler: RouteHandler

      A handler function that will be called when no other route is matched

    Returns this

    A self-reference, suitable for chaining.

get

  • Adds a route matching GET HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

getPlugins

  • getPlugins(): PluginBase[]
  • Returns all plugins, including those registered on destination routers.

    Returns PluginBase[]

head

  • Adds a route matching HEAD HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

match

  • Adds a route matching all methods.

    Example:

     new Router().match('/p/:productId', ({ cache, proxy }) => {
       cache({
         edge: {
           maxAgeSeconds: 60 * 60 * 24
         }
       })
    
       proxy('origin')
     })

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, method, and headers.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

options

  • Adds a route matching OPTIONS HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

patch

  • Adds a route matching PATCH HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

post

  • Adds a route matching POST HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

prerender

put

  • Adds a route matching PUT HTTP method.

    Parameters

    • criteria: RouteCriteria | string

      Either the path as a string or an object with path, headers and cookies.

    • handler: RouteHandler

      A handler function that will be called when the route is matched

    Returns this

    A self-reference, suitable for chaining.

requireBasicAuth

  • requireBasicAuth(options: BasicAuthOptions): this
  • Adds Basic Authentication.

    example

     router
       .requireBasicAuth({ username: 'user', password: 's3cr3t!' })
       // ...include additional routes below...

    You can also require basic auth for a subset of URLs using the criteria option:

     router
       .requireBasicAuth({ criteria: '/secret/:path*', username: 'user', password: 's3cr3t!' })
       // ...include additional routes below...

    The criteria option accepts a path pattern as a string, or a RouteCriteria object.

    Parameters

    • options: BasicAuthOptions

    Returns this

    A self-reference, suitable for chaining.

run

  • Handles a request, sending the response by running the handler for each matched route.

    Parameters

    Returns Promise<void>

static

  • static(sourcePath: string, options?: StaticOptions): this
  • Adds routes for all static assets in a directory tree.

    Example

     router.static('public')

    To only serve files matching a particular pattern, use the glob options:

     router.static('public', {
       glob: '*.css'
     })

    By default the path on which files are served corresponds to the path within the directory, so for example, public/css/main.css would be served at '/css/main.css', you can override this using the path option:

     router.static('public', {
       paths: file => ['/assets/ + file] // will serve public/css/main.css at /assets/css/main.css
     })

    You can add caching or other additional handler logic using the handler option:

     router.static('public', {
       handler: (file) => ({ cache }) => {
         cache({
           browser: {
             maxAgeSeconds: 60 * 60
           },
           edge: {
             maxAgeSeconds: 60 * 60 * 24,
             staleWhileRevalidateSeconds: 60 * 60
           }
         })
       }
     })

    Parameters

    • sourcePath: string

      The path to a directory containing static assets relative to the root of your project.

    • Default value options: StaticOptions = {}

    Returns this

    A self-reference, suitable for chaining.

toString

  • toString(): string
  • Returns a string summarizing all routes for debugging purposes. Don't rely on the format of this string as we may improve it over time.

    Returns string

use

  • use(pluginInstance: PluginBase): this
  • Constructs plugin and pushes it to registered plugins

    Parameters

    • pluginInstance: PluginBase

      A plugin to use.

    Returns this

    A self-reference, suitable for chaining.

Static isRequestFromEdge

  • isRequestFromEdge(req: Request): boolean
  • Returns true if the request came through Layer0 edge.

    Notes:

    • If the request came through Layer0 edge then x-0-version has been injected.

    Parameters

    Returns boolean

Generated using TypeDoc