Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  126] [ 1]  / answers: 1 / hits: 12892  / 8 Years ago, sun, june 26, 2016, 12:00:00

I have the following code that checks to see if a user exists in a database...



router.post('/', function (req, res) {

User.findOne({
username: req.body.log_username,
password: req.body.log_password
}, function (err, docs) {
if (docs.length !== 0) {
console.log(user exists);

}
else {
console.log(no exist);
}
});

});


I have a home page that I want to send the user to if the login was a success. What should I put in the if statement to send the use to another page, in this case, home.js. home.js has the following code in it...



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

router.get('/', function (req, res) {
res.render('home', { title: 'Express' });
});

module.exports = router;

More From » node.js

 Answers
1

You need to redirect the user to main page. res.redirect('path')



Reference : http://expressjs.com/en/4x/api.html#res.redirect



router.post('/', function (req, res) {

User.findOne({
username: req.body.log_username,
password: req.body.log_password
}, function (err, docs) {
if (docs.length !== 0) {
console.log(user exists);
res.redirect('/'); // main page url
}
else {
console.log(no exist);
res.redirect('/login');
}
});

});

[#27802] Friday, June 24, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jesseh

Total Points: 579
Total Questions: 98
Total Answers: 99

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;