将 express/redis 用于会话存储时未定义“会话" [英] 'session' is undefined when using express / redis for session store

查看:19
本文介绍了将 express/redis 用于会话存储时未定义“会话"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 express 应用中使用 redis 进行会话.

I'm trying to use redis for sessions in my express app.

我执行以下操作:

var express = require('express');
var RedisStore = require('connect-redis')(express);

app.configure('development', function(){     
    app.use(express.session({ secret: "password", 
                            store: new RedisStore({
                                          host: "127.0.0.1",
                                          port: "6379",
                                          db: "mydb"
                                        })  
          }));

稍后,在我的应用程序中,如果我执行以下操作:

Later on, in my app, if i do something like:

var whatever = req.session.someProperty;

我明白了:

无法读取未定义的属性someProperty"

Cannot read property 'someProperty' of undefined

这表示 req.session 未定义(我可以从配置部分的 console.log 条目中看到这一点)

This indicates that req.session is undefined (I can see this from a console.log entry in my config section)

我肯定已经运行了 redis,并且可以看到我的应用最初连接到它(使用 redis-cli 监视器)

I've definitely got redis running, and can see my app connects to it initially (using redis-cli monitor)

推荐答案

看来你不见了:

app.use(express.cookieParser());

在您的 app.use(express.session(...)); 调用之前.

请参阅此处.

这篇关于将 express/redis 用于会话存储时未定义“会话"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆