midware
midware is a tiny module to createsimple middleware layers for any node.js or browser application. Inspired in the middleware pattern by connect.
It's only ~80 SLOC.
Example
var use = var message = {} use
Installation
Node
To install midware in a Node application use npm.
npm install midware
Browser
bower install midware
component install h2non/midware
<script src="//cdn.rawgit.com/h2non/midware/0.1.7/midware.js"></script>
Test
To run tests use npm.
$ npm install
$ npm test
Documentation
Basic Usage
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 midware = var use =
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.
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.
use
run
takes any amount of parameters that the callbacks will passed whenever run.
use
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.
use
Apply Context
Instead of binding context to callbacks, send the context to midware
.
var context = {}var use = use
Removing a function
You can remove registered functions in the middleware via its function name or function reference
var use = use // by function name
var use = { }use // by function reference
API
midware([context])
use(callback...)
use.remove(name|function)
use.run([args...], [done])
use.flush()
use.stack = [ function... ]
License
Copyright (c) 2014 Christopher Turner Copyright (c) 2015 Tomas Aparicio