How to redirect to another page in node.js [duplicate]
You should return the line that redirects
return res.redirect('/UserHomePage');
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
In another way you can use window.location.href="your URL"
e.g.:
res.send('<script>window.location.href="your URL";</script>');
or:
return res.redirect("your url");