Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Home

teknopaul edited this page · 4 revisions
Clone this wiki locally

ServletsFilter style request chaining.

Provides the filter pattern for web servers based on node.

The chain is configured in code, you can externalize it if you need.

Usage

Includes the filter-chain module's constructor

var FilterChain = require("filter-chain").FilterChain;

Load a set of filters (details below how to write filters)

var attributesFilter =      require("../server/attributes-filter"),
    logRequestFilter =      require("../server/log-request-filter")
    serverHeaderFilter =    require("../server/server-header-filter"),
    notModifiedFilter =     require("../server/not-modified-filter"),
    routerFilter =          require("../server/router");

Create an ordered array of the filters and pass it to the FilterChain constructor.

var chainModules = [
    attributesFilter, 
    logRequestFilter, 
    serverHeaderFilter,
    notModifiedFilter,
    routerFilter
];

var chain = new FilterChain(chainModules);

Then, in a server start, the chain with it's execute() method, passing the request and response.

http.createServer(function(request, response) {
    // N.B a typical router should be in the chain
    //router.route(request, response);

    chain.execute(request, response);

}).listen(8000);

That's it, nothing special, but a chaining is an essential design pattern that ought to be done in a consistent manner.

Writing filters

Filters look identical to Java Servlets filters.

var parse = require('url').parse;

filter = function(request, response, chain) {

    var url = parse(request.url);
    console.log(request.method + " " + url.pathname);

    chain.doFilter(request, response);

};

exports.filter = filter;

You must export a function called filter.

Something went wrong with that request. Please try again.