Javascript 使用express会话和passport节点无法正确重定向页面

Javascript 使用express会话和passport节点无法正确重定向页面,javascript,node.js,session,express,Javascript,Node.js,Session,Express,我正在使用passport local和express会话,但我的问题是,当我输入一个地址时,在我关闭会话后,我遇到了浏览器“页面未正确重定向”的问题,因此我要做的是重定向到地址/登录,而不是显示该问题 我会留下我的密码 Routes.js app.get('/index', isLoggedIn, function(req, res) { console.log(req.session); if (req.session == null || req.session

我正在使用passport local和express会话,但我的问题是,当我输入一个地址时,在我关闭会话后,我遇到了浏览器“页面未正确重定向”的问题,因此我要做的是重定向到地址/登录,而不是显示该问题

我会留下我的密码

Routes.js

app.get('/index', isLoggedIn, function(req, res) {
    console.log(req.session);
        if (req.session == null  || req.session == undefined)
        {
             res.redirect('/login');
        }
        else
        {
        res.render('index.ejs', {
            user : req.user // get the user out of session and pass to template
        });
    }

    });
app.js

 app.use(session({
     secret  : 'asjdknas',

 }));


 app.use(passport.initialize());
 app.use(passport.session());
 app.use(flash());
更新已解决

验证我在app.get call中调用的函数

函数isLoggedIn(请求、恢复、下一步){

If(req.isAuthenticated()) 返回next()

Res.redirect('/login');
}

尝试使用
返回res.redirect
而不是
res.redirect
并告诉我发生了什么。(渲染时相同)


另外,
console.log(req.session)
提供了什么?

尝试使用
return res.redirect
而不是
res.redirect
并告诉我发生了什么。(渲染时相同)


另外,
console.log(req.session)
给了您什么?

在passport中,有一个内置函数,req.isAuthenticated()

在passport中,有一个内置函数,req.isAuthenticated()

返回相同的事件=(,console.log在使用会话会话{Cookie:{Path:'/',u expires:null,OriginalMaxAge:null,HttpOnly:true},Passport:{user:1}这意味着
req.session
不是
null
undefined
,因此您没有输入重定向条件。但我想您以前尝试并成功地输入了该条件?您应该添加一个console.log('Hello')在你的
res.redirect
之前检查你是否确实在这里489/5000我刚刚检查过,问题是它没有进入,错误是“页面没有正确重定向”控制台只返回这个GET/index 302 12686 ms-56 GET/index 302 2300 ms-56 GET/index 302 1232 ms-56 GET/index 302 0.906 ms-56 GET/index 302 0.729 ms-56 GET/index 302 0.551 ms-56 GET/index 302 0.795 ms-56 GET/index 302 1430 ms-56 GET/index 302 1189 ms-56 GET/index 302 2.079 ms-56 GET/index 302 0.706 ms-56 GET/index 302 0.664 ms-56非常感谢解决方案是将si放在函数isloggedHow中你怎么做?我在这里是新手:(如果你解释,我将使用return the sample ocks=(,console.log在使用会话{Cookie:{Path:'/',u expires:null,OriginalMaxAge:null,HttpOnly:true},Passport:{user:1}这意味着
req.session
不是
null
undefined
,因此您没有输入重定向条件。但我想您以前尝试并成功地输入了该条件?您应该添加一个console.log('Hello')在你的
res.redirect
之前检查你是否确实在这里489/5000我刚刚检查过,问题是它没有进入,错误是“页面没有正确重定向”控制台只返回这个GET/index 302 12686 ms-56 GET/index 302 2300 ms-56 GET/index 302 1232 ms-56 GET/index 302 0.906 ms-56 GET/index 302 0.729 ms-56 GET/index 302 0.551 ms-56 GET/index 302 0.795 ms-56 GET/index 302 1430 ms-56 GET/index 302 1189 ms-56 GET/index 302 2.079 ms-56 GET/index 302 0.706 ms-56 GET/index 302 0.664 ms-56非常感谢解决方案是将si放入函数IsLoggedH中,您是如何做到的?我是新来的:(如果您解释,我会这么做