middleware-flow
Middleware control flow library
Installation
npm install middleware-flow
Examples
series(middlewares...)
var series = require('middleware-flow').series;
var app = require('express')();
app.use(series(mw1, mw2, mw2));
parallel(middlewares...)
var parallel = require('middleware-flow').parallel;
var app = require('express')();
app.use(parallel(mw1, mw2, mw2));
parallelWait(middlewares...)
var parallelWait = require('middleware-flow').parallelWait;
var app = require('express')();
app.use(parallelWait(mw1, mw2, mw2));
each
var each = require('middleware-flow').each;
var arr = [1,2,3];
var app = require('express')();
app.use(
each(arr,
function (eachReq, res, next) {
},
function (item, req, eachReq, res, next) {
})
);
or(middlewares...)
var or = require('middleware-flow').or;
var app = require('express')();
app.use(or(user.isOwner, user.isModerator));
and(middlewares...)
Same as series.
if(value).then(middlewares...).else(middlewares...)
var if = require('middleware-flow').if;
var app = require('express')();
app.use(
if(true)
.then(one, two, three)
.else(error)
);
syncIf(fn).then(middlewares...).else(middlewares...)
var syncIf = require('middleware-flow').syncIf;
var app = require('express')();
app.use(
syncIf(nameQueryExists)
.then(one, two, three)
.else(error)
);
function nameQueryExists (req, res) {
return exists(req.query.name);
}
function exists (val) {
return val !== null && val !== undefined;
}
asyncIf(fn).then(middlewares...).else(middlewares...)
var asyncIf = require('middleware-flow').asyncIf;
var or = require('middleware-flow').or;
var fs = require('fs');
var app = require('express')();
app.use(
asyncIf(bodyFileExists)
.then(one, two, three)
.else(other)
);
function logExists (req, res, cb) {
fs.exists(req.body.file, function (exists) {
cb(null, exists);
});
}
mwIf(middleware).then(middlewares..).else(middlewares..)
var mwIf = require('middleware-flow').mwIf;
var app = require('express')();
app.use(
mwIf(userIsModerator)
.then(one, two, three)
.else(other)
);
function userIsModerator (req, res, next) {
if (!req.user.isModerator) {
next(new Error('access denied'));
}
else {
next();
}
}
try(middlewares..).catch(middlewares..)
var flow = require('middleware-flow');
var app = require('express')();
app.use(
flow.try(saveUser)
.catch(rollback)
);
function saveUser (req, res, next) {
db.save(req.user, next);
}
bg(middlewares...)
app.use(
flow.bg(mw1, mw2, mw2)
);
License
MIT