简体   繁体   中英

Can't get express to work with Redis

I'm using Express 4.x . I declare Redis here:

var app = require('express')()
, express = require('express')
, connect = require('connect')
, http = require('http').Server(app)
, path = require('path')
, request = require("request")
, io = require('socket.io')(http);


// Cookie and Session Reqs:
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var sessionStore = new RedisStore();

And I set up my session :

app.use(session({
    secret: "xxxxx",
    store: sessionStore,    
    key: 'sid',
    cookie: {secure: false, maxAge: 600000},
}));

And I use my session variable in one of my routes :

app.get('/public/', function(req, res) {
    req.session.sessionAccessCode = "not set yet";
    res.redirect("index.html");
});

When I remove the store: sessionStore line, there are no problems and I can test that the session variable works. But if I have it, I'm given an error:

TypeError: Cannot set property 'sessionAccessCode' of undefined

Any ideas why Redis isn't working?

You need to start redis first somehow. Depending on how you installed redis, it could be as simple as starting a redis service if you installed via a package for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM