Skip to content

Middlewares

thiagobustamante edited this page Jun 19, 2017 · 7 revisions

This will allow you to create middlewares. They are functions that allow you to customize (by code) the behavior of the each API feature, such as Routing, Authentication, Throttling and so on. You can define middlewares for:

  • Cors - you can define white and black lists, specific rules and so on;
  • Request and Response - allow you to customize your requests and responses information, such as headers, content body and others;
  • Throttling - you to create a key to identify the source of the request;
  • Authentication - allows you to instantiate any passportjs strategy to satisfy a custom authentication method;
  • Circuit Breaker - allows you to customize actions whenever some event occurs, such as 'open', 'close' or 'rejected'.

When defining a middleware, you must create a .js file that exports the middleware function, like:

module.exports = function (request, response) {
  return req.query.denyParam !== '1';
};

Sometimes, your middlewares can need to receive parameters to be initialized. Those middlewares need to be exported as a function that receives the option parameter and return the initialized middleware, as:

module.exports = function(options) {
  return function (request, response) {
    return options.allowedValues.indexOf(req.query.denyParam) < 0;
  };
};

Clone this wiki locally