-
Notifications
You must be signed in to change notification settings - Fork 43
Middlewares
Thiago da Rosa de Bustamante edited this page Mar 27, 2018
·
7 revisions
Middlewares are functions that allow you to customize some step of the request pipeline, such as Routing, Authentication, Throttling and so on.
Tree Gateway support different kinds of middleware:
- Filter - run before the request pipeline and decide if the request should or not be processed;
- Cors - you can define white and black lists, specific rules and so on for handle cor requests;
- Request and Response interceptors - allow you to customize your requests and responses information, such as headers, content body and others;
- Throttling - allow you to create a custom way to identify the source of your requests, to be used by the rate limit engine;
- Authentication - allows you to instantiate any passportjs strategy to satisfy a custom authentication method;
- Service Discovery - Integrate a service discovery provider, like consul, to your request pipeline to define the target of your service requests;
- Circuit Breaker - allows you to customize actions whenever some circuit breaker event occurs, such as 'open', 'close' or 'rejected'.
- Error Handler - allow you to customize all error messages.
- Request Logger - allow you to customize waht to do with your logs collected during requests processing.
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.
- have a property
factory=1
exported too.
Example:
module.exports = function(options) {
return function (request, response) {
return options.allowedValues.indexOf(req.query.denyParam) < 0;
};
};
module.exports.factory = true;