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
- Description
- Example
- Methods
- compose() ⇒
function
- Contribution and License Agreement
- License
- References
This module allows to join middlewares together:
var compose = require('connect-composer')
var req = { test: [] }
var res = {}
var middlewares1 = [
function (req, res, next) { req.test.push('one'); next() },
function (req, res, next) { req.test.push('two'); next() }
]
var middlewares2 = [
function (req, res, next) { req.test.push('three'); next() },
function (req, res, next) { req.test.push('four'); next() }
]
// create a new middleware
var newMiddlewares = compose(middlewares1, middlewares2)
// run new composed middleware
newMiddlewares(req, res, function () {
console.log(req.test) // < [ 'one', 'two', 'three', 'four' ]
})
You can also stack composed middlewares:
var compose = require('connect-composer')
var req = { test: [] }
var res = {}
// pass as Array
var middlewares1 = compose([
function (req, res, next) { req.test.push('one'); next() },
function (req, res, next) { req.test.push('two'); next() }
])
// or by Argument
var newMiddlewares = compose(
middlewares1,
function (req, res, next) { req.test.push('three'); next() },
function (req, res, next) { req.test.push('four'); next() }
)
// run new composed middleware
newMiddlewares(req, res, function () {
console.log(req.test) // < [ 'one', 'two', 'three', 'four' ]
})
Traps errors and catches errors within middlewares (prevents server from crashing)
var compose = require('connect-composer')
var req = { test: [] }
var res = {}
var middlewares = compose(
function (req, res, next) { req.test.push('one'); next() },
function (req, res, next) {
next('badly') // middleware calls `next` with error parameter
},
function (req, res, next) {
req.test.push('two') // is never called
next()
},
function (err, req, res, next) { // error is trapped here; function has arity 4
console.log(err + ' trapped') // < badly trapped
next() // continue with the processing
},
function (req, res, next) { req.test.push('three'); next() },
function (req, res, next) {
if (1) throw new Error('another error') // middleware calls `next` with error parameter
next()
},
function (req, res, next) {
req.test.push('four') // is never called
next()
}
)
// run new composed middleware
middlewares(req, res, function (err) {
console.log(err) // < [Error: another error] is catched
console.log(req.test) // < [ 'one', 'three' ]
})
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 = require('connect-composer')
var res = {}
var initial = {
two: function (req, res, next) { req.test.push('two'); next() },
four: function (req, res, next) { req.test.push('four'); next() }
}
var others = {
one: function one (req, res, next) { req.test.push('one'); next() },
three: function three (req, res, next) { req.test.push('three'); next() },
five: function othersFive (req, res, next) { req.test.push('five'); next() },
six: function six (req, res, next) { req.test.push('six'); next() },
seven: { seven: function (req, res, next) { req.test.push('seven'); next() } },
eight: function (req, res, next) { req.test.push('eight'); next() }
}
// create a composed middleware
var composed = compose(initial)
// do some manipulation
composed.unshift(others.one) // prepend
composed.push(others.five) // append
composed.before('four', others.three) // insert before
composed.after('othersFive', others.six) // insert after
composed.after('six', others.seven)
// named functions become named middleware functions
console.log(composed.stack) // [ { one: [Function: one] },
// { two: [Function] },
// { three: [Function: three] },
// { four: [Function] },
// { othersFive: [Function: othersFive] },
// { six: [Function: six] },
// { seven: [Function] } ]
// lets clone the middlewares
var composed2 = composed.clone() // clone the middlewares; same as `compose(composed)`
composed2.remove('six').remove('two').remove('four') // remove middlewares
// do some more manipulation
composed.replace('seven', others.eight) // replace middleware seven with eight
// run new composed middleware
var req = { test: [] }
composed(req, res, function () {
console.log(4, req.test) // < [ 'one', 'two', 'three', 'four', 'five', 'six', 'eight' ]
})
// run the other composed middleware (with a different request)
var req2 = { test: [] }
composed2(req2, res, function () {
console.log(5, req2.test) // < [ 'one', 'three', 'five', 'seven' ]
})
Run the examples above with node test/sample.js.
## 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
No operation middleware - just calls next
Kind: static method of compose
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.
Copyright (c) 2015 commenthol (MIT License)
See LICENSE for more info.