Node.js 实现集群会导致应用程序在Heroku中登录失败
我的Node express应用程序部署在Heroku(Hobby Dyno)上。数据库连接到Mongo Atlas(免费层M0) 我使用Throng作为集群解决方案,在本地一切正常。但是在heroku上,如果process.env.WEB_CONCURRENCY var设置为>1,那么应用程序只会在登录后重定向回/login。如果我将上述变量设置为1,那么应用程序也可以正常工作 因此,它将上述变量设置为2、3等,从而导致上述行为。我还直接尝试了PM2和集群api,但它总是导致/login重定向回/login,这对我来说意味着一旦实现了工作线程,登录就开始失败 这是登录路径Node.js 实现集群会导致应用程序在Heroku中登录失败,node.js,express,heroku,mongoose,mongodb-atlas,Node.js,Express,Heroku,Mongoose,Mongodb Atlas,我的Node express应用程序部署在Heroku(Hobby Dyno)上。数据库连接到Mongo Atlas(免费层M0) 我使用Throng作为集群解决方案,在本地一切正常。但是在heroku上,如果process.env.WEB_CONCURRENCY var设置为>1,那么应用程序只会在登录后重定向回/login。如果我将上述变量设置为1,那么应用程序也可以正常工作 因此,它将上述变量设置为2、3等,从而导致上述行为。我还直接尝试了PM2和集群api,但它总是导致/login重定向
router.post("/login", passport.authenticate("local", {
failureRedirect: "/login",
failureFlash : true
}),function(req, res){
res.redirect('/requirements'); //Test reveals app gets till here!
});
有一个中间件来检查路由/需求的身份验证
middlewareObj.isLoggedIn = function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
req.flash("error", "Please login first!");
res.redirect("/login"); //This is what appears to be causing the redirect.
};
var throng = require('throng');
var WORKERS = process.env.WEB_CONCURRENCY || 5;
var port = process.env.PORT;
throng({
workers: WORKERS,
lifetime: Infinity
}, start);
function start() {
//App.js code here
}
如果Heroku上的process.env.WEB_CONCURRENCY=2,会导致登录失败的原因是什么;但是如果设置为1,一切都正常吗
入口点是app.js
middlewareObj.isLoggedIn = function isLoggedIn(req, res, next){
if(req.isAuthenticated()){
return next();
}
req.flash("error", "Please login first!");
res.redirect("/login"); //This is what appears to be causing the redirect.
};
var throng = require('throng');
var WORKERS = process.env.WEB_CONCURRENCY || 5;
var port = process.env.PORT;
throng({
workers: WORKERS,
lifetime: Infinity
}, start);
function start() {
//App.js code here
}
非常感谢您的帮助!谢谢大家!