Probably the best way to do this is to make some kind of helper or put your own middleware in a chain instead of your functions.
So your code will look like this:
app.post('/example', oneOf(key, someFunctionOne, someFunctionTwo), function(req, res){ if(!req[key]){ return res.send(400, { message: 'error'}); } else{ return res.json(200, { message: 'ok'}); } });
And the assistant should be something like this:
function oneOf (key) { var fns = Array.prototype.slice.call(arguments, 1); var l = fns.length; return function (req, res, next) { var i = 0; function _next () { if (req[key] || i === l) return next(); fns[i](req, res, _next); i += 1; } _next(); } }
If you decide to do this, the code will look like this:
app.post('/example', functionOneOrTwo, function(req, res){ if(!req.someVar){ return res.send(400, { message: 'error'}); } else{ return res.json(200, { message: 'ok'}); } }); function functionOneOrTwo(req, res, next) { someFunctionOne(req, res, function () { if (req.someVar) return next(); someFunctionTwo(req, res, next); }); }
Simple but untested; -)