midware-pool
midware-pool is a tiny module to create a pool of connect-style domain-agnostic middleware layers for any node.js or browser application. It uses midware behind the scenes.
Supports variadic arguments, stack manipulation and middleware inheritance. It's just ~180 SLOC.
To get started, see the example below or the API usage docs.
Example
var pool = var message = {} // Create foo middleware stackpool // Create another middleware stackpool // Run both middlewares stackspool
Installation
Node.js
To install midware-pool in a Node application use npm.
npm install midware-pool
Browser
Via bower:
bower install midware-pool
Via component:
component install h2non/midware-pool
Or loading the script:
Testing
$ npm install
$ npm test
Usage
Basic setup
Middleware is useful for creating a plugin system or configuring anything within an application. To use midware just require it and make a call to the module.
var pool =
This will return a use
function which when passed a callback will add it a waterfall sequence that will be invoked one after the other whenever the middleware is run.
pool
Callbacks are given a next
function which will always be the last argument. Calling next
will tell the middleware to call the next callback in the use sequence or will complete its run. To run the callback sequence call the method run
on the use
function.
pool
run
takes any amount of parameters that the callbacks will passed whenever run.
pool pool
Stopping
Whenever a callback should throw an exception or wish to stop the middleware from running any more calls. Give next
an error or explicitly tell it stop.
pool pool
Apply Context
Instead of binding context to callbacks, send the context to midware
.
var context = {}var pool = pool pool pool
Removing a function
You can remove registered functions in the middleware via its function name or function reference
var pool = poolpool // by function name
var pool = { }poolpool // by function reference
API
pool([parent]) => Pool
Pool#use(name, ...middleware)
Pool#remove(name, middlewareName|function)
Pool#run(name, [args...], [done])
Pool#runParent(name, [args...], [done])
boolean
Pool#registered(name) => Pool#useParent(pool)
Pool#useCtx(ctx)
Pool#flush(name)
Pool#flushAll()
midware
Pool#stack(name) => Pool#pool = { ...name: midware }
midware
pool.midware =>pool.Pool => Pool
License
Copyright (c) 2015 Tomas Aparicio