sharad chauhan sharad chauhan - 14 days ago 7
Javascript Question

Redirect to another page : NodeJs

I have tried almost all the solution provided here, but as I am new to nodejs I am missing some part in my code. Please help me finding it.
On clicking login button i want to redirect my page to another simple page.

Here is my login_authentication.js

window.onload = function() {


var username = document.getElementById("username");
var password = document.getElementById("password");
var loginButton = document.getElementById("login");


loginButton.onclick = function (req,res) {
if ( username.value == "" || password.value == ""){
alert("Please fill out details!");
}else {
//alert(username.value);
username.value = "";
password.value = "";

router.get('/users', function(req, res, next) {
// res.send('respond with a resource');
res.render('homepage', { title: 'Thank You' });
});
}

}

}


Here is app.js

app.use('/', index);
app.use('/users', users); // need to show this page rest is default code


Here is users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
// res.send('respond with a resource');
res.render('homepage', { title: 'Sample' });
});

module.exports = router;


I want to redirect my page when user clicks on login button whose code is in login_authentication. Thanks for any help.

Answer

use window.location="/users" instead of router.get('/users') in login_authentication.js

Comments