connect-composer
Connect and reuse connect/ express middlewares
Compose connect/ express compatible middlewares and reuse or extend them.
Features:
- Stack middlewares
- Use connect middlewares without or with connect, express
- Trap errors within middlewares using
function (err, req, res, next)
functions - Safely catch errors within middlewares
- Modify and reuse existing middlewares
Table of Contents
- Description
- Example
- Methods
- compose() ⇒
function
- Contribution and License Agreement
- License
- References
Description
Stack Middlewares
This module allows to join middlewares together:
var compose = var req = test: var res = {}var middlewares1 = { reqtest; } { reqtest; }var middlewares2 = { reqtest; } { reqtest; }// create a new middlewarevar newMiddlewares = // run new composed middleware
Stack composed middlewares
You can also stack composed middlewares:
var compose = var req = test: var res = {}// pass as Arrayvar middlewares1 = // or by Argumentvar newMiddlewares = // run new composed middleware
Trap and catch errors
Traps errors and catches errors within middlewares (prevents server from crashing)
var compose = var req = test: var res = {}var middlewares = // run new composed middleware
Manipulate and reuse middlewares
Use the following methods to change an existing composed middleware
unshift(middlewares)
prepend middlewares to the front of the stackpush(middlewares)
push middlewares to the end of the stackbefore(selector, middlewares)
insert middlewares beforeselector
after(selector, middlewares)
insert middlewares afterselector
replace(selector, middlewares)
replace middlewares with nameselector
withmiddlewares
remove(selector)
remove middlewares with nameselector
clone()
clone composed middlewares
var compose = var res = {}var initial = { reqtest; } { reqtest; }var others = { reqtest; } { reqtest; } { reqtest; } { reqtest; } seven: { reqtest; } { reqtest; }// create a composed middlewarevar composed = // do some manipulationcomposed // prependcomposed // appendcomposed // insert beforecomposed // insert aftercomposed // named functions become named middleware functionsconsole // [ { one: [Function: one] }, // { two: [Function] }, // { three: [Function: three] }, // { four: [Function] }, // { othersFive: [Function: othersFive] }, // { six: [Function: six] }, // { seven: [Function] } ] // lets clone the middlewaresvar composed2 = composed // clone the middlewares; same as `compose(composed)`composed2 // remove middlewares // do some more manipulationcomposed // replace middleware seven with eight // run new composed middlewarevar req = test: // run the other composed middleware (with a different request)var req2 = test:
Example
Run the examples above with node test/sample.js.
Methods ## compose() ⇒
function
compose a new middleware function from multiple middlewares
Returns: function
- middleware function
Param | Type |
---|---|
function | Array | Object |
- compose() ⇒
function
function
Inserts `middlewares` before each of the named middleware `selector`
If `selector` does not match a named middleware the middleware stack stays the same
Kind: static property of compose
Returns: function
- middleware
Param | Type | Description |
---|---|---|
selector | String |
selector for named middleware |
middlewares | Array | Object |
function
Inserts `middlewares` after each of the named middleware `selector`
If `selector` does not match a named middleware the middleware stack stays the same
Kind: static property of compose
Returns: function
- middleware
Param | Type | Description |
---|---|---|
selector | String |
selector for named middleware |
middlewares | Array | Object |
function
Replaces the named middleware `selector` with `middlewares`
If `selector` does not match a named middleware the middleware stack stays the same
Kind: static property of compose
Returns: function
- middleware
Param | Type | Description |
---|---|---|
selector | String |
selector for named middleware |
middlewares | Array | Object |
function
Removes the named middleware `selector` from the stack
If `selector` does not match a named middleware the middleware stack stays the same
Kind: static property of compose
Returns: function
- middleware
Param | Type | Description |
---|---|---|
selector | String |
selector for named middleware |
function
Appends `middlewares` to the stack
Kind: static property of compose
Returns: function
- middleware
Param | Type |
---|---|
middlewares | Array | Object |
function
Prepends `middlewares` to the stack
Kind: static property of compose
Returns: function
- middleware
Param | Type |
---|---|
middlewares | Array | Object |
Array
decompose `obj` into middleware Array
Named functions names are used as middleware identifiers
Kind: static method of compose
Returns: Array
- - array of middlewares {Object|Array}
Param | Type |
---|---|
middlewares | Object | Array | function |
function
clone the middleware for further manipulation
Kind: static method of compose
Returns: function
- cloned middleware function
compose.noop()
No operation middleware - just calls next
Kind: static method of compose
Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. You are also implicitly verifying that all code is your original work or correctly attributed with the source of its origin and licence.
License
Copyright (c) 2015 commenthol (MIT License)
See LICENSE for more info.