【发布时间】:2013-03-28 12:05:32
【问题描述】:
我正在使用 express.js 创建一个 node.js REST 服务器,作为其中的一部分,我还创建了一个简单的会话系统。我有 3 个模块:
- app.js
- highscoreMan.js
- userSession.js
http://localhost/api/highscores 的 app.js 网址现在使用给定参数调用 userSession:
//Get all highscores
app.get('/api/highscores', function (req, res) {
userSession.checkValidity(req.query['username'], req.query['sessionid'], highscoreMan.getAll(req, res));
});
但是,在 checkValidity 中,我传递的函数会被自动调用:
function checkValidity(username, sessionId, callback) {
userSession.findOne({ userid: username, sessionid: sessionId }, function (err, result) {
if (err) {
console.log(err);
}
if(result) {
callback;
}
});
}
我只想运行正在传递的函数,因为我从数据库中获得了正确的结果(稍后将添加其他检查以用于会话日期等)。我该怎么做?
【问题讨论】:
标签: node.js asynchronous express