'session' is undefined when using express / redis for session store

后端 未结 5 1924
半阙折子戏
半阙折子戏 2020-11-30 06:06

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

I do the following:

var express = require(\'express\');
var RedisStore = require(\'connect-r         


        
相关标签:
5条回答
  • 2020-11-30 06:18

    Things have changed recently with Express 3 / Express 4. Please confirm you are using version 4.

    The complete middleware concept changed. You need to install these middlewares manually. "express-session" is one of the 4.0 middlewares.

    I recommend to read

    http://scotch.io/bar-talk/expressjs-4-0-new-features-and-upgrading-from-3-0 and https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x

    Additionally some users were confused that the github repo itself is named just "session" but

    npm install express-session
    

    is correct.

    0 讨论(0)
  • 2020-11-30 06:30

    Sessions won't work unless you have these 3 in this order:

    app.use(express.cookieParser());
    app.use(express.session());
    app.use(app.router);
    

    I'm not sure if router is mandatory to use sessions, but it breaks them if it's placed before them.

    0 讨论(0)
  • 2020-11-30 06:43

    Looks like you're missing:

    app.use(express.cookieParser());
    

    before your app.use(express.session(...)); call.

    See here.

    0 讨论(0)
  • 2020-11-30 06:44

    Had the same problem, however it was caused by changes in the latest version of express.

    You now need to pass express-session to the function connect-redis exports to extend session.Store:

    var express = require('express');
    var session = require('express-session')
    var RedisStore = require('connect-redis')(session);
    
    0 讨论(0)
  • 2020-11-30 06:44

    I had the same problem. It turned out that redis was simply configured to a different port.

    0 讨论(0)
提交回复
热议问题