attach-ware
Middleware with configuration.
Installation
npm:
npm install attach-ware
attach-ware is also available as an AMD, CommonJS, and globals
module, uncompressed and compressed.
Usage
x.js
:
module.exports = function (ctx, options) {
if (!options.condition) return;
return function (req, res, next) {
res.x = 'hello';
next();
};
}
y.js
:
module.exports = function (ctx, options) {
if (!options.condition) return;
return function (req, res, next) {
res.y = 'world';
next();
};
}
index.js
:
var ware = require('attach-ware')(require('ware'));
var x = require('./x.js');
var y = require('./y.js');
var middleware = attachWare()
.use(x, {'condition': true})
.use(y, {'condition': false})
.run({}, {}, function (err, req, res) {
console.log(res.x);
console.log(res.y);
});
API
AttachWare = attachWare(Ware)
Create a new AttachWare
based on the given middleware constructor.
Parameters:
Returns: Function
.
AttachWare()
Create configurable middleware. Works just like the given
Ware
.
AttachWare#use(attacher[, input...])
Signatures:
attachWare.use(attacher[, input...])
;attachWare.use(attachers[, input...])
;attachWare.use(list)
;attachWare.use(matrix)
.
Parameters:
-
attacher
(Function
) — One attacher.
-
attachers
(Array.<Function>
) — List where each value is an
attacher
;
-
list
(Array
) — List where the first value is an attacher
,
and further values are input
;
-
matrix
(Array
) — Matrix where each entry is a list
.
Invokes attacher
with context
and all input
.
If attacher
returns another function (fn
, which can be synchronous,
asynchronous, or a generator function), that function is added to the
middleware, and will be invoked when run()
is
invoked like normal middleware.
AttachWare#context
The first argument for attach
ers. When this is falsey, the instance
itself is used.
License
MIT © Titus Wormer