Module: libs/routers

libs/routers

Router class handling any HTTP request starting from the route base path.

Version:
  • 1.0

Methods

init(appconf, reqModel, app, locales)

Initializing location of routers directory defined in app.json configuration file.

Name Type Description
appconf Object

JSON object represents application configuration.

reqModel Object optional

Reference to libs/reqmodel object what will be assign to req.model for each router request.

app express optional

Instance of the application server.

locales Object optional

Reference to /lib/locales.

Example

conf/app.json { ... "routers": [ "/src/routers" ] ... }

loadModule(list, callback)

This function verifying and loading routers and controllers.

Name Type Description
list Array

List of directories or files.

callback function

Returns loaded module to the function handler.

routerConf(opt){Object}

This function evaluates the router configuration and initializes routing rules. A router isolates the requests from the client by a route path. The main parameter from the router instances is base name. For handling all routing requests in the same class we may return just name (Example 1).

Each router path may extend implementation of the the business logic in the multiple controllers. What helps to reduce numbers of lines in the same file. For this reason we need to specify the path into controllers directories (Example 2).

IndigoJS routers process five sequential phases derivation/interception/implementation/middleware/integration. For the security reasons indogoJS protects access to derivation and integration phases. The router may handle and prevent propagation to the next phase starting from interception phase (Example 3).

The next phase implementation resolves in router/controller (Example 4).

The middleware phase usually handles all requests to the static resourses (Example 5).

Name Type Description
opt Object | String optional

Return configuration parameters from the router class. In case of opt is undefined the default router pass will assign to root '/route' or you can override by returning base name as string or an object {base:'/myroute'}

See:
Returns:
New router configuration object.
Examples

Example 1

module.exports = function(router) { return '/account'; };

module.exports = function(router) { return { 'base': '/account' }; };

Example 2

module.exports = function(router) { return { 'base': '/account', 'controllers': [ '/src/controllers', '/src/account/controllers' ] }; };

Example 3

module.exports = function() { return { 'base' : '/account', 'intercept': function(req, res, next) { //prevent router and controllers receiving POST requests if (req.method !== 'POST') { next(); } } } };

Example 4

module.exports = function() { router.get('/login', function(req, res) { indigo.render(req, res, '/login.html'); }); };

Example 5

module.exports = function() { return { 'base' : '/account', 'middleware': function(req, res, next) { next(); } } };