I'm using Express and I would like to replace a middleware on runtime (stop running middleware X and start running middleware Y). I found an easy way to do it, but no one here is giving it as an option:
Instead of using the first middleware, xMiddleware:
app.use(xMiddleware);
I'm wrapping it with another middleware, MyMiddlewareWrap:
MyMiddleware = xMiddleware;
MyMiddlewareWrap = function (req, res, next)
{
MyMiddleware(req, res, next)
}
app.use(MyMiddlewareWrap);
Then, when I want to stop running xMiddleware and start running yMiddleware, I'm assigning yMiddleware to MyMiddleware:
MyMiddleware = yMiddleware;
As a result, xMiddleware doesn't run anymore and yMiddleware is running instead. Does anyone see a problem with it? Is it a good solution?