HTTP filters
Contents
HTTP filters
HTTP filters enable pipelined processing of http requests.
Filters
Filters enable you to step into the request pipeline, and execute code at both request and response of the execution pipeline, possibly intercepting the request directly. Similar to an HTTP Controller, standalone filters are triggered based on an export.
As such, to make a filter, it must export a filter
function:
Minimal filter timing the subsequent request
exports.filter = function (req, next) {
var before = new Date().getTime();
var response = next(req); // next(req) hands over the request to the engine pipeline and returns the response
var after = new Date().getTime();
log.info((after - before) + 'ms');
return response;
};
Filter manipulating the request
exports.filter = function (req, next) {
req.requestLogging = true; // Manipulate request
log.info("Request:" + JSON.stringify(req, null, 2));
var response = next(req); // Continue request pipeline
response.responseLogging = true; // Manipulate response
log.info("Response:" + JSON.stringify(response, null, 2));
return response;
};
Filter intercepting the request
exports.filter = function (req, next) {
if (req.headers['X-Auth-Token'] !== 'letMeIn') {
// intercept request pipeline
return {
status: 403
}
}
req.headers['Authenticated'] = true;
return next(req);
};
Filters may currently be wired into the execution pipeline in the following ways:
-
Using a router
-
Using site mappings