koajs / generic-session

koa session store with memory, redis or others.
MIT License
414 stars 65 forks source link

V2.0是否有BUG?external store取不到值? #126

Closed fred1218 closed 4 years ago

fred1218 commented 7 years ago

我原本使用的是v1.11.6,后来升级到v2. 使用改的redisStore koa-redis,为什么取不到session了?? code is below: 为嘛我取不到了呢?v1.x可以,而2就不可用了?

fred1218 commented 7 years ago

koa controller

 router.get('/testsession', async (ctx) => {
        //set session
        ctx.session.name='zhangsan'
        ctx.body = {
            session: ctx.session,
            sessionId: ctx.sessionId
        }
    })
    router.get('/testsession2', async (ctx) => {
        //cannot get the value
        ctx.body = {
            session: ctx.session,
            sessionId: ctx.sessionId
        }
    })

middleware

module.exports = session({

    store: new redisStore({
        host: config.redis.host,
        port: config.redis.port
    }),
    key: 'SFC_SESSIONID', 

    beforeSave  : function (ctx, session) {
        console.log('+++++before save++++++',ctx.sessionId,session)
    },
    cookie: {
        // 设置session的生命周期
        maxAge: 1000 * 60 * 60 * 24 * 1
    },

    errorHandler: function (err, type, ctx) {
        console.log('----\n', err, type, ctx)
    }