Router

server. Router

Handles the Express routing functionality

Constructor

new Router(root, parentRouter, routes)

Source:
If passing an ExpressRouter as the parentRouter, it is assumed that the Express Router is the top of the router 'heirarchy' (which will have an impact of some of the Router methods)
Parameters:
Name Type Description
root String Route API endpoint for this router
parentRouter Router | ExpressRouter Parent to mount router
routes Array.<Route> Array of routes

Members

childRouters :Array.<Router>

Source:
List of sub-routers
Type:
  • Array.<Router>

expressRouter :external:ExpressRouter

Source:
Express router instance
Type:
  • external:ExpressRouter

handlerMiddleware :Array.<function()>

Source:
Middleware stack to be added before route handlers (useful if you need access to specific request attributes that don't exist when standard middleware runs)
Type:
  • Array.<function()>

map :Object

Source:
Returns the map of routes attached to this router
Type:
  • Object

parentRouter :ExpressApp|Router

Source:
Express router instance
Type:
  • ExpressApp | Router

path :String

Source:
Generates this router's path from its ancestors
Type:
  • String

root :String

Source:
The root route the router will be mounted at
Type:
  • String

routerMiddleware :Array.<function()>

Source:
Middleware stack to be added directly to the router
Type:
  • Array.<function()>

routes :Array.<Route>

Source:
Routes config
Type:
  • Array.<Route>

url

Source:
Returns the URL for the router

Methods

addHandlerMiddleware(…func) → {AbstractApiModule}

Source:
See:
Adds middleware to be called prior to any route handlers. Accepts multiple params. Useful if you need access to specific request attributes that don't exist when standard middleware runs.
Parameters:
Name Type Attributes Description
func function <repeatable>
Middleware function(s) to be added
Returns:
This instance, for chaining
Type
AbstractApiModule

addMiddleware(…func) → {AbstractApiModule}

Source:
See:
Adds middleware to the router stack. Accepts multiple params.
Parameters:
Name Type Attributes Description
func function <repeatable>
Middleware function(s) to be added
Returns:
This instance, for chaining
Type
AbstractApiModule

addRoute(…route) → {AbstractApiModule}

Source:
Store route definition. Accepts multiple params.
Parameters:
Name Type Attributes Description
route Route <repeatable>
Config of route(s) to be added
Returns:
This instance, for chaining
Type
AbstractApiModule

createChildRouter(root, routes) → {Router}

Source:
Creates and adds a sub-router to this router.
Parameters:
Name Type Description
root string The root of the child router
routes Array.<Route> Array of Routes to add
Returns:
The new router instance
Type
Router

flattenRouters(router) → {Array}

Source:
Creates an array defining the router inheritance hierarchy
Parameters:
Name Type Description
router Router The root router
Returns:
Type
Array

getHandlerMiddleware(router, middleware) → {Array.<function()>}

Source:
Recursively gets middleware of the current router heirarchy
Parameters:
Name Type Description
router Router The current router (used when run recursively)
middleware Array.<function()> Middleware function(s) (used when run recursively)
Returns:
Type
Array.<function()>

init()

Source:
Initialises the API

log(level, …args)

Source:
Logs a message
Parameters:
Name Type Attributes Description
level String Level of log
args * <repeatable>
Arguments to be logged

validateRoute(r) → {Boolean}

Source:
Function for filtering bad route configs
Parameters:
Name Type Description
r Route Route config
Returns:
Type
Boolean

warnOnInited(message) → {Boolean}

Source:
Shortcut for checking Router has initialised, logging a warning if not
Parameters:
Name Type Description
message String Message to log on error
Returns:
Type
Boolean