Router class.

Hierarchy

Constructors

Properties

_lookupIndex: undefined | RouteIndex = undefined
_parent: undefined | WeakRef<Route> = undefined
_root: undefined | WeakRef<Router> = undefined
cache: null | Cache<Plan> = ...

Routing cache.

children: Route[] = []

The children of this route, used for nesting routes.

conditions: Record<string, RouteCondition> = {}

Contains all available conditions.

controllerPaths: string[] = []

Directories to look for controllers in, first one has the highest precedence.

controllers: Record<string, any> = {}

Already loaded controllers.

customName: undefined | string = undefined

Custom route name, if defined.

defaultName: undefined | string = undefined

Default route name.

methods: string[] = []

Restrict HTTP methods this route is allowed to handle, defaults to no restrictions.

pattern: Pattern = ...

Pattern for this route.

requirements: Record<string, any>[] = []

Activate conditions for this route.

types: Record<string, PlaceholderType> = ...

Registered placeholder types, by default only num is already defined.

underRoute: boolean = false

Allow under semantics for this route.

websocketRoute: boolean = false

Activate websocket semantics for this route.

Accessors

  • get parent(): undefined | Route
  • The parent of this route.

    Returns undefined | Route

  • set parent(parent): void
  • Parameters

    • parent: undefined | Route

    Returns void

  • get root(): undefined | Router
  • Return the Router object this route is a descendant of.

    Returns undefined | Router

  • set root(root): void
  • Parameters

    Returns void

Methods

  • Returns RouteIndex

  • Parameters

    Returns Promise<null | Plan>

  • Parameters

    Returns Promise<boolean>

  • Add a child to this route.

    Parameters

    Returns Route

  • Register a condition.

    Parameters

    • name: string
    • fn: RouteCondition

    Returns Router

  • Register a placeholder type.

    Parameters

    Returns Router

  • Generate route matching any of the listed HTTP request methods or all.

    Parameters

    Returns Route

    Example

    // Route with pattern and destination
    route.any('/user').to('User#whatever');

    // Route with HTTP methods, pattern, restrictive placeholders and destination
    route.any(['DELETE', 'PUT'], '/:foo', {foo: /\w+/}).to('Foo#bar');

    // Route with pattern, name and destination
    route.any('/:foo').name('foo_route').to('Foo#bar');

    // Route with pattern, condition and destination
    route.any('/').requires({agent: /Firefox/}).to('Foo#bar');

    // Route with pattern and a closure as destination
    route.any('/:foo', async ctx => ctx.render({text: 'Hello World!'}));
  • Generate route matching only DELETE requests.

    Parameters

    Returns Route

    Example

    // Route with destination
    route.delete('/user').to('User#remove');
  • Match routes and dispatch to actions with and without controllers.

    Parameters

    Returns Promise<boolean>

  • Find child route by name, custom names have precedence over automatically generated ones.

    Parameters

    • name: string

    Returns null | Route

  • Generate route matching only GET requests.

    Parameters

    Returns Route

    Example

    // Route with destination
    route.get('/user').to('User#show');
  • Check if this route has a WebSocket ancestor and cache the result for future checks.

    Returns boolean

  • Check if this route qualifies as an endpoint.

    Returns boolean

  • Find route by name and cache all results for future lookups.

    Parameters

    • name: string

    Returns null | Route

  • The name of this route, defaults to an automatically generated name based on the route pattern. Note that the name current is reserved for referring to the current route.

    Parameters

    • name: string

    Returns Router

  • Generate route matching only OPTIONS requests.

    Parameters

    Returns Route

    Example

    // Route with destination
    route.options('/user').to('User#overview');
  • Generate route matching only PATCH requests.

    Parameters

    Returns Route

    Example

    // Route with destination
    route.patch('/user').to('User#update');
  • Plot a route.

    Parameters

    • spec: RouteSpec

    Returns Promise<null | Plan>

  • Generate route matching only POST requests.

    Parameters

    Returns Route

    Example

    // Route with destination
    route.post('/user').to('User#create');
  • Generate route matching only PUT requests.

    Parameters

    Returns Route

    Example

    // Route with destination
    route.put('/user').to('User#replace');
  • Remove route from parent.

    Returns Router

  • Render route with parameters into a path.

    Parameters

    • values: {} = {}

      Returns string

    • Activate conditions for this route. Note that this automatically disables the routing cache, since conditions are too complex for caching.

      Parameters

      • condition: string
      • requirement: Record<string, any>

      Returns Router

    • Suggested HTTP method for reaching this route, GET and POST are preferred.

      Returns string

    • Set default parameters for this route.

      Parameters

      • Rest ...targets: (string | Record<string, any> | MojoAction)[]

      Returns Router

    • Generate route for a nested route with its own intermediate destination.

      Parameters

      Returns Route

      Example

      // Intermediate destination and prefix shared between two routes
      const auth = app.under('/user').to('User#auth');
      auth.get('/show').to('User#show');
      auth.post('/create').to('User#create');
    • Prepare controllers for routing.

      Returns Promise<void>

    • Generate route matching only WebSocket handshake requests.

      Parameters

      Returns Route

      Example

      // Route with destination
      app.websocket('/echo').to('Example#echo');

    Generated using TypeDoc