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 = require('midware-pool')()
var message = {}
pool.use('foo', function(msg, next) {
next()
})
pool.use('bar', function(msg, next) {
next()
})
pool.run('foo', message, function(err) {
if (err) return console.log(err)
pool.run('bar', message, function(err) {
console.log('End')
})
})
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:
<script src="//cdn.rawgit.com/h2non/midware-pool/0.1.2/midware-pool.js"></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 = require('midware-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.use('foo', function(next) {
next()
})
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('foo', function (err, end) {
if (err) return console.log(err)
if (end) console.log(end)
})
run
takes any amount of parameters that the callbacks will passed whenever run.
pool.use('foo', function (first, last, next) {
console.log('Hello %s, %s', first, last)
next()
})
pool.run('foo', 'Chunk', 'Norris')
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.use('foo', function(next) {
next(new Error())
next(null, true)
})
pool.run('foo', function (err, ended) {
})
Apply Context
Instead of binding context to callbacks, send the context to midware
.
var context = {}
var pool = midwarePool()
pool.useCtx(context)
pool.use('foo', function (next) {
next()
})
pool.run('foo', function(err) {
})
Removing a function
You can remove registered functions in the middleware via its function name or function reference
var pool = midwarePool()
pool.use('foo', function test(next) {
next()
})
pool.remove('foo', 'test')
var pool = midwarePool()
function test(next) {
next()
}
pool.use('foo', test)
pool.remove('foo', test)
API
pool([parent]) => Pool
Pool#use(name, ...middleware)
Pool#remove(name, middlewareName|function)
Pool#run(name, [args...], [done])
Pool#runParent(name, [args...], [done])
Pool#registered(name) => boolean
Pool#useParent(pool)
Pool#useCtx(ctx)
Pool#flush(name)
Pool#flushAll()
Pool#stack(name) => midware
Pool#pool = { ...name: midware }
pool.midware => midware
pool.Pool => Pool
License
MIT
Copyright (c) 2015 Tomas Aparicio