【问题标题】:In a node.js app using express, how can one call response.redirect from within a mongoose query?在使用 express 的 node.js 应用程序中,如何从 mongoose 查询中调用 response.redirect?
【发布时间】:2014-02-11 03:49:33
【问题描述】:

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

res.redirect('/lobby') 在 mongoose 查询 (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 输出截图:

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

【问题讨论】:

  • 您是否在重定向呼叫之前立即尝试console.log(something)
  • 是的。在重定向调用之前和之后。打印出来很好。
  • 在中间件中调用next() 之前的console.log 怎么样?
  • 是的,它也在那里输出。它还在路由函数内部输出,在 res.sendfile 之前和之后...

标签: javascript node.js express mongoose


【解决方案1】:
        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
        }

【讨论】:

  • 我在帖子中编辑了我的代码以显示 console.log 调用,并添加了 node.js 输出的屏幕截图以及 Firefox 控制台中显示的请求详细信息。一切似乎都执行得很好......除了在我的浏览器中,重定向没有发生。如果我在浏览器栏中手动输入localhost/lobby,路由可以正常工作,身份验证也可以。
  • 您正在执行 ajax XmlHttpRequest。这些不会像正常的浏览器页面加载那样自动进行重定向。您必须根据服务器响应的 Location 标头在浏览器 js 中手动更改 window.location
  • 哦,好吧...没想到这对服务器端的事情有影响。谢谢!
猜你喜欢
  • 2013-10-17
  • 2015-10-21
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 2022-06-15
  • 1970-01-01
相关资源
最近更新 更多