Nodejs会话不会持续生产

时间:2016-06-23 18:47:22

标签: javascript node.js session express

我不能为我的生活解决为什么我的会话或cookie不会持久存在于网址

之内

表达

app.use(session({
    secret: 'somethign secrect',
    resave: true,
    saveUninitialized: true,
    cookie: {
        path: '/',
        domain: config.cookieDomain,
        httpOnly: true,
        secure: false,
        maxAge: null,
        cookie: { domain:config.cookieDomain}
    },
    rolling: true
}));

url / auth

                //Store cookies for 1 hour
                console.info(options.req.session); //output 1
                options.req.session.HUTBER = 'JAMIE'; 
                console.info(options.req.session); //output 2

** url / auth / callback **

    console.info('auth', req.session); //output 3

终端输出

//Output 1
@40000000576c2d6d061dbe14 Session {
@40000000576c2d6d061dc1fc   cookie: 
@40000000576c2d6d061dc1fc    { path: '/',
@40000000576c2d6d061dc1fc      _expires: null,
@40000000576c2d6d061dc5e4      originalMaxAge: null,
@40000000576c2d6d061dc5e4      httpOnly: true,
@40000000576c2d6d061dc9cc      domain: 'mysite.co.uk',
@40000000576c2d6d061dc9cc      secure: false,
@40000000576c2d6d061dcdb4      cookie: { domain: 'mysite.co.uk' } } }

//Output 2
@40000000576c2d6d0624e61c Session {
@40000000576c2d6d0624ea04   cookie: 
@40000000576c2d6d0624ea04    { path: '/',
@40000000576c2d6d0624edec      _expires: null,
@40000000576c2d6d0624edec      originalMaxAge: null,
@40000000576c2d6d0624f1d4      httpOnly: true,
@40000000576c2d6d0624f1d4      domain: 'mysite.co.uk',
@40000000576c2d6d0624f5bc      secure: false,
@40000000576c2d6d0624f5bc      cookie: { domain: 'mysite.co.uk' } },
@40000000576c2d6d0624f9a4   HUTBER: 'JAMIE' }

//Output 3
@40000000576c2d7c14dfd57c auth Session {
@40000000576c2d7c14dfdd4c   cookie: 
@40000000576c2d7c14dfdd4c    { path: '/',
@40000000576c2d7c14dfe134      _expires: null,
@40000000576c2d7c14dfe134      originalMaxAge: null,
@40000000576c2d7c14dfe51c      httpOnly: true,
@40000000576c2d7c14dfe51c      domain: 'mysite.co.uk',
@40000000576c2d7c14dfe904      secure: false,
@40000000576c2d7c14dfecec      cookie: { domain: 'mysite.co.uk' } } }

如何在两条路线上获取数据?

1 个答案:

答案 0 :(得分:1)

首先,您使用会话中间件的默认会话存储。默认会话存储将会话信息存储在内存中,仅适用于开发和测试目的。如果您正在使用多个应用程序实例和负载均衡器(您通常在生产中期望的那样),那么默认会话存储将不适合您。我建议您使用其他商店,例如connect-redis或其他将会话信息保存到专门商店的商店。