Connect-Chain-If
"connect-chain-if" is a middleware that composes a new middleware of type function(req, res, next)
from a chain of middlewares to be called in series.
The new middleware is fully connect compliant.
An error passed to next
or thrown in a middleware will short circuit the chain and be
passed to the next error middleware of type function(err, req, res, next)
in the connect middleware chain.
You can use "connect-chain-if" to build up new middlewares from existing connect-style-middlewares using the conditional expressions chain.if()
and chain.switch()
.
To allow breaking up long lasting middleware chains, all middlewares within a chain are processed via the node event loop using process.nextTick
instead of being processed synchroniously. Use chain.nextTick(false);
to revert to the synchronous behaviour.
Usage
Building new middlewares
var express = chain = ; var app = { resbody = requrl + '\n'; next && ; } { res; res; res; next && ; }; var newMiddleware = ;// or to maintain backward compatibilitynewMiddleware = ; // then use it in express/ connectapp; app;
Building middlewares with error traps
var chain = ; var { // ... something bad happened console; next && ; } { console; next && ; } { console; next && ; } { // note the "4" arguments console; next && ; }; next && ; }; {}{};
In this example middleware2
will be bypassed. Output is:
middleware1
trapped error: error
middleware3
Using connect middlewares without connect/ express
var http = chain = ; var { if /^\/[a-z]?$/ resbody = requrl; next && ; else next && ; } { res; res; res; } { res; res; }; http ;
NOTE: Take care that in this case your very last middleware in the chain
handles any error.
curl -v localhost:3000/a#> < HTTP/1.1 200 OK #> /a curl -v localhost:3000/aaa#> < HTTP/1.1 404 Not Found
Creating conditional middlewares
You can either use chain.if
or chain.switch
to define conditional middlewares.
var chain = ; var { req res { next && ; };}; ;//> 200 'homepage';//> 500 'weired';//> 404 'no idea'
Using chain.switch
:
var chain = ; var { req res { next && ; };}; ;//> 200 'homepage';//> 500 'weired';//> 404 'no idea'
License
Software is released under MIT.