编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

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

Khang-NT redis 2022-5-11 11:02 8人围观

腾讯云服务器
使用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会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^