co-ware
v1.6.0
Published
Ware inspired, easily create your own middleware layer using generators via co.
Downloads
2,688
Readme
co-ware
Ware inspired, easily create your own middleware layer using generators via co.
Examples
var ware = require('..');
var w = ware()
.use(function *(next) {
this.x = 'hello';
yield next;
})
.use(function *(next) {
this.y = 'world';
yield next;
})
.use(function *(next) {
yield next;
});
w.run({}, {}, function *() {
console.log(this.x, this.y);
});
Print the arguments of input.
var ware = require('..');
var w = ware()
.use(function *(next) {
console.log(this.input); // 1, 2, 3
yield next;
});
w.run(1, 2, 3);
Handles error.
var ware = require('..');
var w = ware()
.use(function *(next) {
if ('42' != this.input[0].life) throw new Error();
yield next;
})
.use(function *(next) {
console.log('yes!');
})
.on('error', function (err) {
console.log('no!');
});
w.run({ life: '41' }); // "no!"
w.run({ life: '42' }); // "yes!"
API
ware()
Create a new list of middleware.
.use(*fun)
Push a middleware fn(GeneratorFunction) onto the list. If the middleware has an arity of one more than the input to run it's an error middleware.
.run(input..., [GeneratorFunction])
Runs the middleware functions with input... and optionally calls callback(GeneratorFunction).
.clear()
Clear the middleware.
License
MIT