2

I'm trying get access to session data in express so I thought I would try declaring a connect-redis session store when configuring express. However, I cannot see why this doesn't work:

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

var app = express();
app.set('port', process.env.PORT || 3000);
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat", store: new RedisStore }));
//app.use(express.session({ secret: "keyboard cat" }));
app.use(app.router);

app.get('/', function(req, res){
    console.log('/');
    req.session.items = [ 'apple', 'orange' ];
    res.end('items configured');
});

app.get('/items', function(req, res){
    console.log('/items: ', req.session.items);
    var s = JSON.stringify(req.session.items);
    res.end('items: ' + s);
});

var server = http.createServer(app).listen(app.get('port'), function(){
    console.log('Express server listening on port ' + app.get('port'));
});

The '/' route simply configures items with the session.

The '/items' route displays the list of items in the session.

It works using the standard expressjs session store. It doesn't work using connect-redis (req.session is undefined)

I'm assuming the redis store will be instantiated and destroyed as the app loads/unloads (or do I need it running outside of node/express app?)

Any ideas?

1
  • add node.js tag into your question to get more attention/views Commented Nov 27, 2013 at 13:41

3 Answers 3

4

req.session will be undefined if RedisStore can't connect to your Redis server. So it's either not running, or it's not running on the default location that RedisStore is looking for it (127.0.0.1:6379).

In case of the latter, you can configure the location using the options argument to the RedisStore constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah, I thought redis server was launched by node - I see it's a separate server process. I'll use expressjs MemoryStore as the store instead of running up a new server.
1

Give this a try.

var express = require('express');
var redis   = require("redis");
var session = require('express-session');
var redisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var client  = redis.createClient();
var app = express();

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);

app.use(session({
    secret: 'ssshhhhh',
    // create new redis store.
    store: new redisStore({ host: 'localhost', port: 6379, client: client,ttl :  260}),
    saveUninitialized: false,
    resave: false
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.get('/',function(req,res){  
    // create new session object.
    if(req.session.key) {
        // if email key is sent redirect.
        res.redirect('/admin');
    } else {
        // else go to home page.
        res.render('index.html');
    }
});

app.post('/login',function(req,res){
    // when user login set the key to redis.
    req.session.key=req.body.email;
    res.end('done');
});

app.get('/logout',function(req,res){
    req.session.destroy(function(err){
        if(err){
            console.log(err);
        } else {
            res.redirect('/');
        }
    });
});

app.listen(3000,function(){
    console.log("App Started on PORT 3000");
});

link : https://codeforgeek.com/2015/07/using-redis-to-handle-session-in-node-js/

Comments

0

You should invoke RedisStore constructor (with ())

app.use(express.session({ secret: "keyboard cat", store: new RedisStore()}));

2 Comments

I do prefer the parentheses too, though. If only to shut up jshint ;)
Yeah - prefer () myslef but copied/pasted code from somewhere else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.