req.session undefined with express和connect-redis

时间:2012-12-22 14:37:25

标签: javascript node.js session express redis

  

可能重复:
  ‘session’ is undefined when using express / redis for session store

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

  debug: true,
  ttl: 90000000,
  connection: {port: '7170', host: '127.0.0.1'}    

});

app.configure(function(){

   app.use(express.bodyParser());
   app.set('views',__dirname + '/views');
   app.set('view engine', 'ejs');
   app.use(express.static(__dirname + '/public'));
   app.use(express.session({ secret: 'p!550ff', store: new RedisStore }));
   app.use(express.cookieParser());
   app.use(app.router);

});

使用此配置,只要我调用req.session,nodemon就会告诉我它未定义。我想,express.cookieParser()express.session已经很好地宣布了。任何解决方案......?

1 个答案:

答案 0 :(得分:3)

cookieParser中间件需要在session中间件之前,所以交换顺序如下:

   app.use(express.cookieParser());
   app.use(express.session({ secret: 'p!550ff', store: new RedisStore }));

请参阅此here上的Connect文档。