Skip to content
Ivaylo Ivanov edited this page Jan 16, 2016 · 4 revisions

Defined in Esrol-middlewares and implemented in Esrol-server-app

The Middlewares provides a functionality to access the request, the response, and to call the next middleware function in the application’s request-response cycle. They behave similar to Express.js middlewares. If a request can be handled by certain route, the router passes the request and the response to the middlewares.

The Middlewares are called successively, the order is determined by the priority property - the lower, the better. Each middleware must export onRequest method and priority property with int value.

Currently, only HTTP middlewares are functional.

How to use it:

app/middlewares/http-middlewares/some-middleware.js

'use strict';

module.exports = class SomeMiddleware {

  static get priority() {
    return 1;
  }

  static onRequest(req, res, next) {
    // some code
    next();
  }

};

You can use third party modules as middleware as well. Example of commonly used middleware in Express.js:

app/middlewares/http-middlewares/body-parser.js

'use strict';
let parser = require('body-parser').urlencoded({extended: true});

module.exports = class BodyParser {

  static get priority() {
    return 1;
  }

  static onRequest(req, res, next) {
    return parser(req, res, next);
  }

};

Clone this wiki locally