Nodejs表示session memorystore无法找到具有给定Id的会话

时间:2012-08-26 12:05:23

标签: node.js express session-cookies

我已经学习了如何创建一个带有cookie解析的节点表达应用程序和一个MemoryStore作为Session存储的教程。但在安装了某些模块的最新版本后,我的应用程序不再正常工作了。昨天我安装了最新版本的“express”,“connect”和“cookie”,现在我再也无法将这些会话从MemoryStore中删除了。

以下是我为重现问题而设置的简单应用:

server.js -----------

var express = require('express');
var MemoryStore = express.session.MemoryStore;
var sessionStore = new MemoryStore();
var connect = require('connect');
var Session = connect.middleware.session.Session;
var cookie = require('cookie');

module.exports.startServer = function() {

var app = express();

// Configuration
app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session({
        store : sessionStore,
        secret : 'secret',
        key : 'express.sid'
    }));
    app.use(express.static(__dirname + '/public'));
    app.use(app.router);
});

app.configure('development', function() {
    app.use(express.errorHandler({
        dumpExceptions : true,
        showStack : true
    }));
});

app.configure('production', function() {
    app.use(express.errorHandler());
});



// Init routes
app.post('/login', function(req, res){
    var credentials = req.body;

    if (!(credentials.username && credentials.password)){
        res.redirect('/login.html');
        return;
    } 

    if (credentials.username === 'user1' && credentials.password === 'pass1'){
        req.session.user = credentials.username;
        req.session.clientId = credentials.clientId;
        res.redirect('/post-message.html');
    }else{
        req.session.destroy();
        res.redirect('/login.html');
    }
});

app.post('/postMsg', authenticate, function(req, res){
    res.send('posted');
});

app.listen(4000);

function authenticate(req, res, next) {
    // check if there's a cookie header
    if (req.headers.cookie) {
        // if there is, parse the cookie
        req.cookie = cookie.parse(req.headers.cookie);

        req.sessionID= req.cookie['express.sid'];
        // note that you will need to use the same key to grad the
        // session id, as you specified in the Express setup.

        sessionStore.get(req.sessionID, function(err, session) {

            if (session && session.user) {
                // save the session data and accept the connection
                req.session = new Session(req, session);
                next();
            }
            else {
                //Turn down the connection
                res.redirect('/login.html');
            }
        });
    } else {
        // if there isn't, turn down the connection with a message
        // and leave the function.
        res.redirect('/login.html');
    }
}
};

我可以在调试器中看到一切似乎都正常工作,直到我试图通过调用“/ postMsg”路由发布消息。然后它进入“authenticate”函数并尝试从sessionStore获取带有“req.sessionID”的会话。这不再成功,sessionStore.get为会话返回undefined。但是如果我使用调试器查看sessionStore,我可以看到商店中有一个会话,它似乎也与sessionID匹配。

有谁知道我的剧本出了什么问题?

感谢您的帮助!

0 个答案:

没有答案