The Routing Unit

libflitter/routing/RoutingUnit.js

exports:  class RoutingUnit

class RoutingUnit

extends:  libflitter/Unit

Loads the route definition files from the specified and creates Express routers for each file, then registers them with the underlying Express app. Creates a global function to return a redirect response.

constructor(String router_directory = './app/routing/routers')

overrides:  super.constructor()         returns:  this

Initializes the class. Resolves the fully-qualified path of the router_directory and stores it in this.directory.

go(express app, function next)

overrides:  super.go()         returns:  undefined

Creates an alias function to make returning redirections easier. Then, it reads the files in this.directory, non-recursively and loads the routes defined in each valid file. Using these routes, it creates an Express router for each file and registers it with the underlying Express app. This establishes the Routing context and calls the next function in the stack under it.

provides()

overrides:  super.provides()         returns:  Object

Returns an Object describing the services, templates, and directories provided by this unit. They are as follows:

Type Key Value Notes
Service Name name "routing" The name of the service. This is added to the _flit.services array so other units can check for this unit.
Directory directories.routers this.directory The fully-qualified path of the router files. This is added to the _flit.directories store so other units can access it.
Template templates.router Object Template that creates new config file in this.directory.
directory

type:  String

Set on initialization. Specifies the fully-qualified path of the route definition files to be processed. Each of these files should export an Object of key-value pairs that hold the route definition information for that file. Each file's configuration is assigned a name based on the name of the file it was retrieved from. Unlike other units, this does not include sub-directories. Only files in the top-level folder are parsed.

Routing Context

Loads the routes from the definition files in the specified directory and makes them available to the underlying Express app. Guarantees that the routes have been registered and are present in the _flit.routing.routes object. Also provides utility functions.

_flit.routing

type:  Object

Holds the variables and functions provided by this unit.

_flit.routing.routes

type:  Object

Set of key-value pairs such that the key is the name of the route definition file parsed from the top-level directory only, and the value is the Object of route definition information from that file.

_flit.routing.redirect(String destination)

returns:  mixed

Returns a handler function that will send a response redirecting the user to the given destination URL. Meant to be used in the route definition files, like so:

// creates a handler function for Flitter that sends a redirect to 'http(s)://app.url/dashboard'
'/dash': [ redirect('/dashboard') ]
global.redirect(String destination)

returns:  mixed

Mapped to _flit.routing.redirect().

No Comments
Back to top