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?

4
  • 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) Commented Jan 28, 2014 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. Commented Jan 28, 2014 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. Commented Jan 28, 2014 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. Commented Jan 28, 2014 at 22:40

1 Answer 1

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?

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

Comments

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.