Middleware

Middleware is a way of running one or more processes before the controller is executed. This is helpful for deciding on how a request should be handled before the controller runs. For example should this request be ajax only? If so, a middleware could be added to the route that sends the user a 400 error if the request isn't sent via ajax.

Defining Middleware

The following will perform middleware before the request is handled.

module.exports = {
  handle (client) {
    // Perform the task
    return true
  }
}

However, you may need to perform the task after the request is handled which can be done like this.

module.exports = {
  postHandle (client) {
    // Perform the task
    return true
  }
}

Middleware Types

There are two types of middleware:

  1. Global middleware
  2. Route middleware

Global Middleware

Global middleware is middleware that runs on every request. This can be used for sessions, rate limiting or whatever you need to run on every request.

This middleware is not setup in the same way that route middleware is setup, as it is added to /config/middleware.js.

const { Session, Cookies } = require('../app/middleware')

module.exports = {
  middleware: [Session, Cookies]
}

Route Middleware

Route middleware is more common than global middleware, and is use on a per-route basis. This is added to either the group method or one of the provided method type methods (Router.get(), Router.post(), etc.)

const { Ajax } = require('horsepower')

Router.get('/', { middleware: [Ajax] }, client => client.response.html('Hello World'))

Instead of defining the same middleware on every route, a group can be used to define middleware on a group of routes.

const { Ajax } = require('horsepower')

Router.group({ middleware: [Ajax] }, () => {
  Router.get('/ping', client => client.response.html('pong'))
  Router.get('/pong', client => client.response.html('ping'))
})

Middleware Groups

Sometimes middleware needs to be grouped into groups because writing out the same middleware repeatedly can get tiresome. These middleware groups are than executed like macros.

By editing /config/middleware.js a group of middleware can be created for use.

module.exports = {
  middlewareGroups: {
    session: [Cookies, Session],
    apiAuth: [Ajax, Auth]
  }
}

Then you can use that group within your routes by passing the value as a string instead of a reference to the middleware option:

Router.get('/ping', { middleware: ['apiAuth'] }, () => {})