如何将用户重定向到' home'用数据库验证登录数据后的页面?

时间:2017-12-10 14:44:45

标签: mysql node.js

检查登录数据(用户名和密码)后,我想将有效用户重定向到主页。

这是app。交:

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

    req.checkBody('home_user_name', 'User Name is required').notEmpty();
    req.checkBody('home_password', 'Password is required').notEmpty();


pool.getConnection(function (error, tempCont) {
                if (!!error) {
        tempCont.release();
                    console.log('Error');
                }
                else {
                    console.log('Connected!');


                function get_role(callback) {


         console.log("look here >> " + req.body.home_user_name);

这里是查询:

   tempCont.query('SELECT * from `users` where `user_name` = ? ', [req.body.home_user_name] , function (error, results) {
                    if (error) callback(null);
                    callback(results[0].password);
                });
            }

这是如何将有效用户重定向到主页的问题?请注意,它是app.post中的函数内部函数:

 get_role(function (data) {
                    if (data == req.body.home_password){


                    console.log("User Name is " + req.body.home_user_name + " and Password is " + data);
                 }
                    else {
                        console.log("passwords are not identical ");
                    }
                }
                );

这里是错误验证:

   var errors = req.validationErrors();
            if (errors) {
                res.render('app', { errors: errors });
            } else {
                console.log("Validation: " + req.body.home_user_name);
            }
        }
    })
    })

1 个答案:

答案 0 :(得分:0)

这是解决方案:

res.redirect ('home');