Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect to another page in node.js [duplicate]

Tags:

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> 
like image 918
aiden87 Avatar asked Apr 05 '16 19:04

aiden87


People also ask

How do I redirect a URL in node?

redirect() function, we can now discuss how to redirect back to original URL in NodeJS. Back redirect: We can use this method to redirects the request back to the referrer. If no referrer is present, the request is redirected to “/” route by default.

How do you repeat a node js code?

You use repeating to print repeating strings: const repeating = require('repeating'); console. log(repeating(100, 'unicorn '));

How do I redirect a login page in node JS?

session. user = o; res. redirect('/home'); } else{ res. render('/login', { title: 'Hello - Please Login To Your Account' }); } }); } });


2 Answers

You should return the line that redirects

return res.redirect('/UserHomePage'); 
like image 139
Rob Brander Avatar answered Sep 20 '22 04:09

Rob Brander


Ok, I'll try to help you using one my examples. First of all, you need to know I am using express for my application directory structure and for creating files like app.js in an automatically way. My login.html looks like:

... <div class="form"> <h2>Login information</h2> <form action="/login" method = "post">   <input type="text" placeholder="E-Mail" name="email" required/>   <input type="password" placeholder="Password" name="password" required/>   <button>Login</button> </form> 

The important thing here is action="/login". This is the path I use in my index.js (for navigating between the views) which look like this:

app.post('/login', passport.authenticate('login', {     successRedirect : '/home',      failureRedirect : '/login',      failureFlash : true }));  app.get('/home', function(request, response) {         response.render('pages/home'); }); 

This allows me to redirect to another page after a succesful login. There is a helpful tutorial you could check out for redirecting between pages:

http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/

To read a statement like <%= user.attributes.name %> let's have a look at a simple profile.html which has the following structure:

<div id = "profile"> <h3>Profilinformationen</h3>     <form>         <fieldset>             <label id = "usernameLabel">Username:</label>             <input type = "text" id="usernameText" value = "<%= user.user.username %>" />             <br>         </fieldset>     </form> 

To get the the attributes of the user variable, you have to initialize a user variable in your routing.js (called index.js in my case). This looks like

app.get('/profile', auth, function(request, response) {     response.render('pages/profile', {         user : request.user     }); }); 

I am using mongoose for my object model:

var mongoose = require('mongoose'); var bcrypt   = require('bcrypt-nodejs'); var role     = require('./role');  var userSchema = mongoose.Schema({     user             : {         username     : String,         email        : String,         password     : String     } }); 

Ask me anytime for further questions... Best regards, Nazar

like image 31
Nazar Medeiros Avatar answered Sep 21 '22 04:09

Nazar Medeiros