0

I am trying to utilize a Redis-based session store using connect-redis, communicating over UNIX Domain Sockets.

There is this: Redis Connection via socket on Node.js but the answer is specific to node-redis, and not the connect-redis for Redis session stores.

I thought it would be easy to get things going by creating my own node-redis object using and passing in a 'client' parameter, as described in the 'Options' section of the README here: https://github.com/visionmedia/connect-redis

But, when I do so, the req.session parameter never gets set on the Express application.

var Redis = require('redis');
var RedisStore = require('connect-redis')(express);
var redis = Redis.createClient(config['redis']['socket']);

And the session component:

app.use(express.session({
    store: new RedisStore({client: redis})
}));

Am I missing something?

Community
  • 1
  • 1
Bobby
  • 1,439
  • 2
  • 16
  • 30
  • Have you linked in `express.cookieParser()`? Sessions won't work without it (there has to be some way to store the session ID, which is normally in a cookie) – Ethan Brown Jan 28 '14 at 22:08
  • Yes. Apologies; I stripped everything down to what I felt was applicable. I actually have a working setup if you remove "client: redis" from the RedisStore piece. – Bobby Jan 28 '14 at 22:29
  • Also, "redis-cli -s /tmp/redis.sock" does work, though I had file system permissions problems the first time I tried. Fixing those didn't help. – Bobby Jan 28 '14 at 22:34
  • (nod) Just checking...Express middleware documentation doesn't always do a great job of specifying dependencies. Well, everything _looks_ right, and I'm afraid I don't have time to dive into it now...I'll try to come back to this question later. – Ethan Brown Jan 28 '14 at 22:40

1 Answers1

1

Looks like you're doing everything right.... I put together a small proof-of-concept, and it works for me. Here's my complete code:

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

var redisClient = redis.createClient( 17969, '<redacted>.garantiadata.com', 
    { auth_pass: '<redacted>' } );

var app = express();

app.use(express.cookieParser('pure cat flight grass'));
app.use(express.session({ store: new RedisStore({ client: redisClient }) }));
app.use(express.urlencoded());

app.use(app.router);

app.get('/', function(req, res){
    var html = '';
    if(req.session.name) html += '<p>Welcome, ' + req.session.name + '.</p>';
    html += '<form method="POST"><p>Name: ' +
        '<input type="text" name="name"> ' +
        '<input type="submit"></p></form>';
    res.send(html);
});

app.post('/', function(req, res){
    req.session.name = req.body.name;
    res.redirect('/');
});

http.createServer(app).listen(3000,function(){
    console.log('listening on 3000');
});

I'm using a free Redis account from Garantia Data, not that that should make a difference. Do you spot any differences between what I'm doing and what you're doing?

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92