使用connect-redis和Unix域套接字的Node.js Express会话 [英] Node.js Express sessions using connect-redis with Unix Domain Sockets

查看:50
本文介绍了使用connect-redis和Unix域套接字的Node.js Express会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 connect-redis 利用基于Redis的会话存储,并通过UNIX域套接字进行通信.

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

存在以下内容: Redis通过Node.js上的套接字进行连接,但答案仅针对 node-redis ,而不是针对Redis会话存储的 connect-redis .

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.

我认为,通过使用并传入'client'参数创建自己的 node-redis 对象可以轻松解决问题,如自述文件的选项"部分: https://github.com/visionmedia/connect-redis

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

但是,当我这样做时, req.session 参数永远不会在Express应用程序上设置.

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']);

以及会话组件:

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

我想念什么吗?

推荐答案

看起来您所做的一切都正确无误....我整理了一个小的概念证明,它对我有用.这是我完整的代码:

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');
});

我正在使用Garantia Data提供的免费Redis帐户,但这不应该有所作为.您发现我在做什么与在做什么之间有什么区别吗?

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?

这篇关于使用connect-redis和Unix域套接字的Node.js Express会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆