在使用express的node.js应用程序中,如何从mongoose查询中调用response.redirect?

时间:2014-02-08 23:29:36

标签: javascript node.js express mongoose

我正在使用Express和Mongoose编写我的第一个Node.js应用程序。我的代码的相关摘录如下。

从猫鼬查询(res.redirect('/lobby'))中调用

models.User.findOne,但不起作用。请注意,在编写mongoose查询之前,每一段代码,包括数据库的读取,重定向和checkAuth函数都能正常工作。代码按原样执行(我使用控制台输出检查)并且不会抛出任何错误,但实际的重定向不会发生!

//Middleware

function checkAuth(req,res,next){
    console.log("Test output 4");
    if(!req.session.user_id){
        res.sendfile(path.join(root,'public/no_auth.html'));
    }else{
        console.log("Test output 5");
        next();
        console.log("Test output 8");
    }
};

//Routing

app.get('/lobby',checkAuth,function(req,res){
    console.log("Test output 6");
    res.sendfile(path.join(root,'lobby.html'));
    console.log("Test output 7");
});

//Login & Registration

app.post('/login',function(req,res){
    var post=req.body;
    if(post.user=='' || post.password=='' || post.platform==''){
        res.send(200,{success:false,details:'empty'});
    }else if(post.platform!=platform_password){
        res.send(200,{success:false,details:'platform'});
    }else{
        models.User.findOne({'name':post.user},'name password',function(err,user){
            if(err){
                console.info(err);
            }else if(post.password!=user.password){ //TODO figure out hash and salt stuff
                res.send(200,{success:false,details:'password'});
            }else{
                console.log("Test output 1: "+user.name);
                req.session.user_id=user.name;
                console.log("Test output 2: "+req.session.user_id);
                res.redirect('/lobby'); //<- this doesn't work
                console.log("Test output 3");
            }
        });
    }
});

Node.js输出截图:

enter image description here

Firefox控制台中的请求/响应详细信息的屏幕截图:

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

        if(err){
            console.info(err);
            //You need to send a response here.
            //Each request must always get exactly one response no matter
            //how your code logic branches.
            //Otherwise, this request will just hang until timeout, which is bad
            res.send(500, err);
            //also, using early returns instead of a big if/else tree is cleaner IMHO
        }else if(post.password!=user.password){ //TODO figure out hash and salt stuff
            res.send(200,{success:false,details:'password'});
        }else{
            req.session.user_id=user.name;
            //This should work just fine.
            //In the comments you claim a console.log before and after this print
            //out but this "doesn't work".
            //1. I have good reason to be dubious of your claims here.
            // Clear your mind and double check yourself.
            //2. Get some technical detail beyond "Doesn't work".
            // Use curl and see what happens. Does the request just hang unresponded?
            res.redirect('/lobby'); //<- this doesn't work
        }