NodeJS – 显示login或不是用户的不同内容

我试图显示不同的内容login,而不是在一个页面上的用户。

这是我用来生成/页面的代码:

app.get('/',function(req, res){ if (!checkSession(req, res)) { res.render('index.ejs', { title: 'FrontSpeak - blog-based social network' }) } else { res.render('index.ejs', { title: 'autrhorized' }) } }) 

checkSession函数:

 function checkSession(req, res) { if (req.session.user_id) { db.collection('users', function (err, collection) { collection.findOne({ _id: new ObjectID(req.session.user_id) }, function (err, user) { if (user) { req.currentUser = user; return true; } else { return false; } }); }); } else { return false; } } 

loginfunction:

 app.post('/', function(req, res){ db.collection("users", function (err, collection) { collection.findOne({ username: req.body.username }, function (err, doc) { if (doc && doc.password == req.body.password) { console.log("user found"); req.session.user_id = doc._id; } } }); }); }); 

所以,它似乎并没有工作。 不过,我认为这不是显示不同内容的最佳方式。 可能有一些更优雅的方法来做到这一点? 谢谢!

更新:新的loginfunction:

 app.post('/', function(req, res){ db.collection("users", function (err, collection) { collection.findOne({ username: req.body.username }, function (err, doc) { console.log('found user'); if (doc && doc.password == req.body.password) { req.session.user_id = doc._id; res.redirect('/'); }; res.redirect('/'); }); res.redirect('/'); }); }); 

这是尝试将传统的同步模型应用于Node的asynchronouscallback驱动模型的一种情况。

数据库查询完成后,您返回true ,但您只是返回到数据库驱动程序。 checkSession很久以前就返回了。 由于该函数返回未定义,如果有一个session.user_id (和假如没有),login检查将始终评估为false。

相反,你可以使用Brandon的build议使checkSessionasynchronous,或者我推荐实现一个中间件function:

 function checkLogin(req, res, next) { if (req.session.user_id) { db.collection('users', function (err, collection) { if (err) return next(err); // handle errors! collection.findOne({ _id: new ObjectID(req.session.user_id) }, function (err, user) { if (user) { req.currentUser = user; } else { req.currentUser = null; } next(); }); }); } else { req.currentUser = null; next(); } } 

现在有两种使用中间件function的方法。 如果你想检查每个请求的用户,只需将其添加到应用程序:

 app.use(checkLogin); 

现在每一个请求都会有一个req.currentUser ,但是每次请求都会导致从数据库中获取login状态的性能。 或者,如果您只需要某些请求的用户信息,请将该function粘贴在路由中:

 app.get('/', checkLogin, function(req, res) { if (req.currentUser) { // logged in } else { // not } }); 

您可以在快速文档中阅读更多关于此的信息 。

它看起来像你试图通过检查它的返回值来使用checkSession作为一个同步函数,但checkSession不能同步,因为它依赖于asynchronousfunction,即callback在这里: db.collection('users', function (err, collection) ...你需要修改checkSession是asynchronous的:

 function checkSession(req, res, callback) { if (req.session.user_id) { db.collection('users', function (err, collection) { collection.findOne({ _id: new ObjectID(req.session.user_id) }, function (err, user) { if (user) { req.currentUser = user; callback(true); } else { callback(false); } }); }); } else { callback(false); } } 

然后在请求处理程序中asynchronous使用它:

 app.get('/',function(req, res){ checkSession(req, res, function(isUser) { if (!isUser) { res.render('index.ejs', { title: 'FrontSpeak - blog-based social network' }) } else { res.render('index.ejs', { title: 'autrhorized' }) } }); })