1.0.0 • Published 8 years ago

@ash-framework/middleware v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 years ago

Middleware support for ash framework

NPM

Usage

Loads middleware from a specified directory or from inline express style middleware functions.

Defining middleware routing

Basic routing

Define a nested function to specify which middleware to load and in what order.

const middleware = function () {
  this.middleware('token')
  this.middleware('user')
}

In the example above, token middleware will be loaded before user middleware.

this.middleware

this.middleware(name|express middleware, [options|callback])

The this.middleware function takes the name of the middleware as the first parameter (or alternatively an express middleware function can be passed to bypass middleware loading and be used instead See Inlining middleware) The second optional parameter is either an options object to be passed to the middleware to be loaded (See Loading middleware) or a callback function to be used for grouping middleware. (See Grouping middleware)

Grouping middleware

Middleware can be grouped using nesting. This has no application other than to help you to group your middleware in a way that makes sense for your application.

const middleware = function () {
  this.middleware('security', function () {
    this.middleware('token')
    this.middleware('csrf')
  })
  this.middleware('user')
}

In the above example, token middleware will run followed by csrf and then finally user. No middleware is run for security itself as this is just a namespace.

Inlining middleware

The this.middleware function can also take an express style middleware callback of the form function (req, res, next) {} The main intended use for this is so that you can load external middleware.

const helmet = require('helmet')

const middleware = function () {
  this.middleware(helmet())
}

or simply

const middleware = function () {
  this.middleware(function (req, res, next) {
    // do something
    next()
  })
}

Loading middleware

Loading middleware involves passing a definition function (See defining middleware routing), an express app instance and a path to a directory of route files to load from.

const express = require('express')
const load = require('@ash-framework/middleware')
const path = require('path')

const middlewareDefinition = function () {
  this.middleware('token')
}
const expressApp = express()
const middlewareDirectory = path.join(__dirname, 'middleware')

load(middlewareDefinition, expressApp, middlewareDirectory)

Defining middleware

When this.middleware(name) is called, the module will attempt to look up a file named name in the directory defined in middlewareDirectory (See Loading Middleware).

const app = express()

load(function () {
  this.middleware('user')
}, app, __dirname + '/middleware')

In the example above, a file name user.js will be looked for and loaded if it exists. (A descriptive error will thrown if it does not).

Loaded files should export a class with a method register

// user.js
module.exports = class Middleware {
  register (httpContext, options) {
    // return a promise if you want subsequent middleware to wait
    return Promise.resolve()
  }
}

httpContext

httpContext is a wrapper for express request and response objects. It is an object with 4 properties/methods as follows.

  • request - express request object
  • response - express response object
  • get(key) - property getting
  • set(key, value) - property setter

If you set something on httpContext in one middleware class, it will be available via get in later middleware classes.

const app = express()

const definition = function () {
  this.middleware('token')
  this.middleware('user')
}
load(definition, app, __dirname + '/middleware')

// token.js
module.exports = class Middleware {
  register (httpContext, options) {
    httpContext.set('token', 'ABC1234')
  }
}

// user.js
module.exports = class Middleware {
  register (httpContext, options) {
    const token = httpContext.get('token') // ABC1234
  }
}

options

options is an object passed through from the middleware definition

const app = express()

const definition = function () {
  this.middleware('token', {type: 'csrf'})
}
load(definition, app, __dirname + '/middleware')

// token.js
module.exports = class Middleware {
  register (httpContext, options) {
    console.log(options.type) // csrf
  }
}

Discover the release history by heading on over to the releases page.

These amazing people are maintaining this project:

These amazing people have contributed finances to this project:

Become a sponsor!

These amazing people have contributed code to this project:

Unless stated otherwise all works are:

and licensed under: