节点 - Passport Auth - Authed Post Route在表单提交时挂起

War*_*ins 5 authentication mongodb node.js express passport.js

这很奇怪.Im Passport的"本地策略"为我的快递应用而且我遇到了一个奇怪的问题.

基本上,我有三条路线.每个人都有一个auth检查到位.

app.get('/admin', authenticatedOrNot, adminRoute.index);
app.get('/admin/new', authenticatedOrNot, adminRoute.newpost);
app.post('/admin/new', authenticatedOrNot, adminRoute.create);
Run Code Online (Sandbox Code Playgroud)

authenticatedOrNot方法很简单:

var authenticatedOrNot = function(req, res, next){
    if(req.isAuthenticated()){
        next();
    }else{
        res.redirect("/login");
    }
}
Run Code Online (Sandbox Code Playgroud)

适用于登录管理区域,并检查用户是否已登录,但是当我将表单提交到'/ admin/new'Post路由时,浏览器挂起.即使使用console.log,控制台也没有任何反应:

exports.create = function(req, res){
    console.log(req);
        // Database logic here
        res.redirect('/admin');
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法让它工作.它只是挂起,最终失败了.浏览器控制台只是在网络请求中说"待定".

香港专业教育学院尝试从后期路线和相同的问题中删除'authenticatedOrNot'方法,但如果我删除所有三个它工作正常.

我很难过.

有帮助吗?其他人遇到这个?

zel*_*oba 0

当您拆分得越来越多时,此类问题的诊断变得更加容易......最好的方法是使用一些嗅探器(内置于 Chrome、Firefox、Opera 或独立版本)并准确获取您发送到服务器的标头。这非常有用,因为您可以将问题定位到前端应用程序(<form acton="/admin/new"例如 \xe2\x80\x93 错误)或后端。

\n\n

让我们道歉,您的标头没问题,并且您在/admin/new路线上发送了正确的 POST 消息。既然你的console.log( req );没有生效,显然应用程序还没有走到这一步。这可能是因为authenticatedOrNot挂起或adminRoute.create没有正确实例化。

\n\n

authenticatedOrNot正如我所见,可能会挂起/login重定向,因为您没有提供处理此路由的方式。

\n\n

adminRoute.create可能会导致一些麻烦,具体取决于您将其附加到应用程序中的方式。

\n\n

所以在简历中我需要看更多你的代码来确定麻烦。

\n