无法将redisStrore连接到express-session

时间:2017-07-26 18:58:58

标签: node.js session express redis node-redis

代码: Express.js



'use strict';

module.exports = function(app, io, server) {

  var express = require('express');
  var bodyParser = require('body-parser');
  var methodOverride = require('method-override');

  app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(methodOverride('X-HTTP-Method-Override'));

  // Redis
  const redis = require('./utils/redis.js')();
  app.set('redis', redis);

  let isSessionCreated = false;
  app.use('*', (req, res, next) => {

    if(!isSessionCreated) {
      isSessionCreated = true;
      require('./middleware/sessionConfig.js')(app);
      next();
    }
  });

  // Passport
  require('./middleware/passport.js')(app);

  // Start Socket
  require('./middleware/startSocket')(app, io);
  
  app.use(express.static(__dirname + '/public'));
  app.use(express.static(__dirname + '/dist'));

  
  // Start Webpack
  require('./webpack/startWebpack.js')(app);

  // routes ==================================================
  require('./routes/routes')(app); // configure our routes

  require('../config/webpack.dev.js');
}




redis.js



'use strict';
const redis         = require('redis'),
      session       = require('express-session'),      
      redisStore    = require('connect-redis')(session),
      redisClient   = redis.createClient(),
      uuid          = require('uuid'),  
      cookieParser  = require('cookie-parser');

module.exports = function() {

  console.log('Redis started');
  // Connects to Redis Store
  const sessionStore = new redisStore({ 
    host: '127.0.0.1',
    port: 6379,
    client: redisClient,
    ttl :  3600000
  });

  redisClient.on('error', (err)=> {
    console.log('could not connect ', err);
  });

  // Checks for Redis Connection
  redisClient.on('connect', ()=> {
    console.log('redis connected');
  });

  return {
    set: function(obj) {
      console.log('set called: ', obj);
      redisClient.set(obj.id, obj.token);
    },

    sessionStore
  }
}




sessionConfig.js



'use strict';
module.exports = function(app) {
  const cookieParser  = require('cookie-parser'),
        session       = require('express-session'),
        uuid          = require('uuid');

  // Sets up Express Session
  app.use(cookieParser());

  console.log('session created');
  let redis = app.get('redis');
  app.use(session({
    name: 'irelief',
    secret: 'irelief',
    store: redis.sessionStore,
    cookie: {
      cookieName: 'xyz',
      httpOnly: false,
      secret: 'irelief',
      secure: false,
      maxAge: 360000000
     },
     genid: function(req) {
      return uuid.v4();
    },
    saveUninitialized: false,
    resave: true,
    rolling: true
  }));
}




在app.get上输出请求(' /',(req,res))

  

会话:会话{     曲奇饼:      {路径:' /',        _expires:2017-07-30T22:38:34.653Z,        originalMaxAge:360000000,        httpOnly:false,        cookieName:' xyz',        秘密:' irelief',        安全:假}}

我尝试过任何社区所说的一切,到目前为止没有任何帮助。 passport.js

还存在同样的问题

任何帮助都会很棒

0 个答案:

没有答案