The Middleware Unit

libflitter/middleware/MiddlewareUnit

exports:  class MiddlewareUnit

class MiddlewareUnit

extends:  libflitter/Unit

The MiddlewareUnit loads and registers middleware from the specified directory and applies global middleware. It makes the middleware globally available under the Middleware context.

constructor(String mw_path = './app/routing/middleware', String global_definitions_file = './app/routing/Middleware.js')

overrides:  super.constructor()         returns:  this

Initializes the class. Resolves the fully-qualified paths for the middleware definitions folder and the file which contains the array of middleware to be applied globally and stores these paths in this.directory and this.globals_file respectively.

go(express app, function next)

overrides:  super.constructor()         returns:  undefined

Recursively iterates over files in this.directory and loads the middleware definition classes from them and stores the collection by name in _flit.middleware.definitions. Then, it creates an access function and stores it in _flit.middleware.mw. Then, global middleware is loaded from the array in this.globals_file and is applied, in order, to the underlying Express app. This creates the Middleware context and the next function in the stack is called 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 "middleware" The name of the service. This is added to the _flit.services array so other units can check for this unit.
Directory directories.middleware this.directory The fully-qualified path of the middleware definition files. This is added to the _flit.directories store so other units can access it.
Template templates.middleware Object Template that creates new middleware definition file in this.directory.
directory

type:  String

Set on initialization. Specifies the fully-qualified path of the middleware definition files to be processed. Each of these files should export a class with a test() function that runs the middleware. Each file's middleware is assigned a name based on the name of the file it was retrieved from. This includes sub-directories. As is convention in Flitter, sub-directories are delineated with a : character. So a middleware class loaded from the file auth/RequireAuth.middleware.js would be named auth:RequireAuth.

globals_file

type:  String

Set on initialization. Specifies the fully-qualified path of the global middleware file. This file should export an Array of references to middleware that will be applied to every request Flitter handles. These should be specified using the global mw() function.

Middleware Context

This context provides access to registered middleware and the global middleware access function. It also guarantees that that all requests will have the global middleware applied to them.

_flit.middleware

type:  Object

Stores variables and functions provided by this context.

_flit.middleware.definitions

type:  Object

A key-value collection of defined middleware where keys are parsed from the names of files in the specified middleware directory, following the Flitter convention for sub-directory naming. The values are classes containing a test() function that is applied as middleware.

_flit.middleware.mw(String name)

Returns the middleware class from _flit.middleware.definitions with the name provided.

global.mw(String name)

Mapped to _flit.middleware.mw().

No Comments
Back to top