How to skip or jump middleware? (node)

2.7k Views Asked by At

Given two middleware, how can you add something that would get hit first, then determine which of the middleware to pass the request onto. The request can only be dropped or forwarded to one of the middleware, never both.

app.use( function *(next) {
    if (isAuthenticated)
        *private middleware*
    else
        *use public middleware*
});

More Specifically:
I have a homepage / that should have a variable response depending on if the user is logged in or not (using token-based auth here). For simplicity, if the request user does not have a token, then public.html is the response while private.html for request bearing a valid token.
Now this would be easy enough to just implement within one function, but I'd like to have separated routers, figure that would keep the code more manageable.
So I need to somehow be able to choose which middleware router the request goes to right? No idea how to do that.

var publicR = new router();
publicR.get('/', function *(next) {
    ....public.html....
});
var privateR = new router();
privateR.get('/', function *(next) {
    ....private.html....
});

app.use(function(){
   if(isAuthenticated)
      ...use privateR.routes();
   else
      ...use publicR.routes();
});
2

There are 2 best solutions below

1
On BEST ANSWER

First off, it's unusual and really not a good idea to present entirely different content for a given URL when logged in or not logged in. So, trying to have two different routers that all serve the same routes, but one is for a logged in user and the other for a not-logged in user is probably just not a good design idea.

The more usual case is to have a portion of a page that might be different when logged in. In that case, you have a single route creating the page that handles doing something slightly different with the content when logged in or not.

If you really want to have completely different content and behavior when logged in, then you should probably redirect to a different URL when logged in. In that case, you can just use an entirely different router for the "logged in" URLs. This will also work better for search engines since a given URL will report consistent content and not be different depending upon the state of the user. This also makes the use of Routers in Express really easy. Your "logged in" router serves the logged in URLs. You can have middleware that checks a logged-in URL to see if you are actually logged in and, if not, redirects back to the non-logged in page and vice versa.

0
On

In case anyone else runs into this issue, this is working for me:

var publicR = new router();
publicR.get('/', function *(next) {
    ....public.html....
});
var privateR = new router();
privateR.get('/', function *(next) {
    ....private.html....
});

app.use(mySwitcher);


function *mySwitcher(next){
   if(isAuthenticated)
      yield privateR.routes().call(this,next);
   else
      yield publicR.routes().call(this,next);
}