【问题标题】:Faile to lookup view even tho everything else is found即使找到其他所有内容也无法查找视图
【发布时间】:2019-06-29 18:45:32
【问题描述】:

我正在创建一个简单的passport-local 登录应用程序。

在我成功登录之前,一切似乎都运行良好。

具体来说,我需要访问只有在成功登录后才能看到的/admin 视图。

这是我的 routes/index.js 文件:

module.exports = function(app, passport) {

    // GET The HOME-page
    app.get('/', function(req, res, next) {
        res.render('index', {
            title: 'Index',
            message: 'Index Page'
        });
    });

    // GET The LOGIN-page
    app.get('/login', function(req, res, next) {
        res.render('login', {
            title: 'Login',
            message: 'Login Page',
            flash_message:  req.flash('loginMessage')
        });
    });


    // Process the login form
    app.post('/login', passport.authenticate('local', {
        successRedirect: '/admin',
        failureRedirect: '/login',
        failureFlash: true 
    }),
    function (req, res) {
        console.log('Hi');
        res.redirect('/');
     });

    app.get('/admin', isLoggedIn, function (req, res) {
        res.render('/admin', {  <-ERROR IS POINTED HERE
            title: 'Admin',
            message: 'Admin page'
        });
    });

    app.get('/logout', function (req, res) {
        req.logout();
        res.redirect('/');
    });
};

// route middleware
function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()) { return next(); }  <-ERROR IS POINTED HERE
    res.status(403).send('[403] Forbidden');
}

console.logged req.isAuthenticated 省略了什么,它省略了 true,所以它应该返回 next()。当然,该页面会给出 500 代码,因此不会调用 403。

我将管理路径更改为没有isLoggedIn 的另一个页面,它可以正常工作,因此我的文件夹结构设置正确。 因此 isLoggedIn 函数搞砸了?但是如何,为什么会说 Failed to lookup,我不太明白。

【问题讨论】:

  • 尝试在您的路线之前放置isLoggedIn 函数。在您的// GET The HOME-page 之前
  • 仍然存在同样的错误,我不认为js像C#那样线性编译代码。

标签: express routing passport.js


【解决方案1】:

所以经过一点调试,我发现这只是一个简单的/错误。

 app.get('/admin', isLoggedIn ,function (req, res) {
        res.render('/admin', {
            title: req.user,
            message: 'Admin page'
        });
    });

res.render('/admin'); 必须更改为 res.render('admin');

因此,我可以得出结论,res.render() 必须按名称查找。

至于app.get('/admin')设置路由,简单来说

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多