Same URL - render different pages (EXPRESS)

Is it possible to render different pages for the same URL in express?

For example, if I click on #login1, I want to be sent to /login_page/. If I click on #login2, I should still be sent to /login_page/. Each time, I want to render different htmls depending on which #login I clicked.

So I want it to look like this.

Client:

    $("#login1").click(function(){
    window.open(/login_page/,'_parent');    
    });

    $("#login2").click(function(){
    window.open(/login_page/,'_parent');    
    });

Server:

    app.get('/login_page/', users.login_page1); //if I clicked #login1
    app.get('/login_page/', users.login_page2); //if I clicked #login2

Thanks a lot for any help.

Basically you need some field in the request to convey this information.

  • The simple thing: the URL, as the web was designed
  • If you're too cool to have the URLs be different, you can use the query string
    • window.open('/login_page?from=login2', '_parent');
  • If you're too cool for the query string, you could set a cookie
  • If you're too cool for a cookie, you could request the page via ajax with xhr.setRequestHeader
  • If you're tool cool for a custom ajax request header, you could add an image with a tracking pixel src attribute to the DOM just prior to loading the login_page and detect that in the server side session and render a different page accordingly

So in summary there are at least a half-dozen ways to technically achieve this. Only the URL and the query string are reasonable, IMHO.

if I got it correctly you just want to invoke different controllers upon the same request with no parameters? you know you can just parametrise the url and get the result you need with small control logic on the server side.

I don't believe it's possible to do this without any parameters. One solution could look like this

client:

$("#login1").click(function(){
window.open(/login_page/1,'_parent');    
});

$("#login2").click(function(){
window.open(/login_page/2,'_parent');    
});

Server:

app.get('/login_page/:id', function(req, res){         //if I clicked #login1
if(req.params.id == 1){
    res.render('index1.html');
}  else {
    res.render('index2.html');
}
}