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

余生颓废 提交于 2020-01-16 19:27:49

问题


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?


回答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?



来源:https://stackoverflow.com/questions/21417585/node-js-express-sessions-using-connect-redis-with-unix-domain-sockets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!