How to redirect to another page in node.js

aiden87 picture aiden87 · Apr 5, 2016 · Viewed 124k times · Source

I have a login and a signup page. When random user wants to login, and login is successful, I want to redirect him to another .ejs page (for example UserHomePage.ejs), however, nothing I've tried have worked so far.

if (loggedIn)
    {
        console.log("Success!");
        res.redirect('/UserHomePage');
    }
    else
    {
        console.log("Error!");
    }

I would also like to know, how to redirect a user on button click.

Let's say I'm on display user page, where I display all of my users, then there is "add another used button". How do i do that? How do I redirect user to Register.js page after onclick?

<h2>List of users</h2>
<ul>
<% uporabniki.forEach(function(user) { %>
<li>  
  <%= user.attributes.name %>
  <%= user.attributes.last name %>
</li>
<% }); %>
</ul>
<h3>Add another user</h3>
<form method="post">
 <input type="submit" value="Add user" />
</form>

Answer

Rob Brander picture Rob Brander · Apr 5, 2016

You should return the line that redirects

return res.redirect('/UserHomePage');