middleware-handler
v0.2.0
Published
Manager for custom middlewares
Downloads
6,310
Readme
MiddlewareHandler
MiddlewareHandler manages custom middlewares in the same way as how Express/Connect does.
var MiddlewareHandler = require('middleware-handler');
handler = new MiddlewareHandler();
handler.use(function(a, b, next()) {
console.log(a, b); // foo bar
next();
});
handler.handle(['foo', 'bar']);
Installation
$npm install middleware-handler
Documentation
#use(middleware)
middleware
accepts variable arguments and a callback.
handler = new MiddlewareHandler();
handler.use(function(arg, next) {
var err;
// do some stuff
next(err); // optionally accepts an error object
});
#clear()
Clear all middlewares from the stack.
handler = new MiddlewareHandler();
handler.use(function() {});
console.log(handler.stack.length); // 1
handler.clear();
console.log(handler.stack.length); // 0
#handle([args], [callback])
Invoke middlewares.
handler = new MiddlewareHandler();
handler.use(function(a, b, next) {
console.log(a, b); // foo bar
next();
});
handler.handle(['foo', 'bar'], function(err) {
// after calling all middlewares
});
#compose([callback])
Create a function that invokes middlewares.
handler = new MiddlewareHandler();
handler.use(function(a, b, next) {
console.log(a, b); // foo bar
next();
});
var fn = handler.compose(function(err) {
// after calling all middlewares
});
fn('foo', 'bar');
compose(middlewares...)
Create a function that invokes the passed middlewares.
function middleware(a, b, next) {
console.log(a, b); // foo bar
next();
}
var fn = MiddlewareHandler.compose(middleware, function(a, b) {
console.log(a, b); // foo bar
});
fn('foo', 'bar');