我有以下代码检查数据库中是否存在用户...
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");
}
});
});
我有一个主页,如果登录成功,我想将其发送到该主页。我应该在 if
语句中放置什么以将使用发送到另一个页面,在本例中为 home.js
。 home.js
中有以下代码...
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('home', { title: 'Express' });
});
module.exports = router;
最佳答案
您需要将用户重定向到主页。 res.redirect('路径')
引用: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');
}
});
});
关于javascript - 登录后如何将用户发送到另一个页面(node js),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38042610/