Bobby
Bobby

Reputation: 1482

Node.js Express sessions using connect-redis with Unix Domain Sockets

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?

Upvotes: 0

Views: 2057

Answers (1)

Ethan Brown
Ethan Brown

Reputation: 27282

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?

Upvotes: 1

Related Questions