There is nothing magical about connect/express ‘middleware’: they are just functions – and you can call them as you would call any other function.
So in your example:
app.use(function(req, res, next){
if (req.query.something) {
middlewareA(req, res, next);
} else {
middlewareB(req, res, next);
}
});
That said, there might be more elegant ways of constructing hierarchical express applications. Check out TJ’s video