在 redis.conf 中使用 requirepass 时,将 Express Node.js 应用程序连接到 Redis 服务器时出错
我是节点和 Redis 的新手,正在设置一个简单的服务器,使用 Express 与 Redis 进行通信。
启动 redis-server 后,我启动节点服务器 app.js
当我使用没有 requirepass 的 redis.conf 文件时,app.js 启动并运行,但是当我添加 requirepass 时,应用程序在启动时崩溃。我可以在终端中运行 redis-cli 并且密码在那里起作用。
运行 app.js 时,我收到此未捕获的错误堆栈:
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Uncaught, unspecified 'error' event.
at RedisClient.emit (events.js:47:15)
at Command.callback (/node_modules/redis/index.js:232:29)
at RedisClient.return_error (/node_modules/redis/index.js:382:25)
at RedisReplyParser.<anonymous> (/node_modules/redis/index.js:78:14)
at RedisReplyParser.emit (events.js:64:17)
at RedisReplyParser.send_error (/node_modules/redis/lib/parser/javascript.js:265:14)
at RedisReplyParser.execute (/node_modules/redis/lib/parser/javascript.js:124:22)
at RedisClient.on_data (/node_modules/redis/index.js:358:27)
at Socket.<anonymous> (/node_modules/redis/index.js:93:14)
at Socket.emit (events.js:64:17)
这是 app.js 中的相关代码
var REDIS_PASS = "foobar";
var express = require('express');
var app = module.exports = express.createServer();
var io = require('socket.io').listen(app);
var redisStore = require('connect-redis')(express);
var redis = require('redis');
var rdb = redis.createClient();
rdb.auth(REDIS_PASS, function(){ console.log('connected to redis-server');});
rdb.on("error",function(err){
console.log("REDIS CLIENT ERROR:" + err + '\n\n' + err.stack);
});
这是 redis.conf 文件
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
#my hackerproof password:
requirepass foobar
我仍在尝试了解节点的异步性质,所以我想这可能是一种情况在建立与 redis 服务器的连接之前调用了 auth 命令,但是当我将 auth 命令放入 client.on("connect" 事件的回调中时,我收到了相同的错误堆栈。
我想知道是否捕获异常将允许服务器将其扫到地毯下并继续,所以我使用进程对象捕获它服务器似乎继续运行,但不会向浏览器提供任何页面,所以我猜这个异常不能被忽略。 。
I'm new to node and redis and am setting up a simple server with express to communicate with redis.
After starting redis-server, I start the node server app.js
app.js starts and runs when I use the redis.conf file without requirepass, but when I add a requirepass the app crashes upon starting. I can run redis-cli in a terminal and the password works there.
I get this Uncaught error stack when running app.js:
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Uncaught, unspecified 'error' event.
at RedisClient.emit (events.js:47:15)
at Command.callback (/node_modules/redis/index.js:232:29)
at RedisClient.return_error (/node_modules/redis/index.js:382:25)
at RedisReplyParser.<anonymous> (/node_modules/redis/index.js:78:14)
at RedisReplyParser.emit (events.js:64:17)
at RedisReplyParser.send_error (/node_modules/redis/lib/parser/javascript.js:265:14)
at RedisReplyParser.execute (/node_modules/redis/lib/parser/javascript.js:124:22)
at RedisClient.on_data (/node_modules/redis/index.js:358:27)
at Socket.<anonymous> (/node_modules/redis/index.js:93:14)
at Socket.emit (events.js:64:17)
Here's the pertinent code in app.js
var REDIS_PASS = "foobar";
var express = require('express');
var app = module.exports = express.createServer();
var io = require('socket.io').listen(app);
var redisStore = require('connect-redis')(express);
var redis = require('redis');
var rdb = redis.createClient();
rdb.auth(REDIS_PASS, function(){ console.log('connected to redis-server');});
rdb.on("error",function(err){
console.log("REDIS CLIENT ERROR:" + err + '\n\n' + err.stack);
});
Here's the redis.conf file
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
#my hackerproof password:
requirepass foobar
I am still trying to get my head around the asynchronous nature of node, so I thought maybe this was a case where the auth command was being called before a connection to the redis-server was established, but when I placed the auth command into a callback for the client.on("connect" event I received the same error stack.
I wondered if catching the exception would allow the server to sweep it under the carpet and continue so I caught it using the process object. The server appears to continue running but won't serve any pages to the browser, so I'm guessing this exception can't be ignored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题是由于使用 RedisStore 对象配置我的应用程序来处理会话引起的。在 redis.conf 文件中启用密码后,需要创建 RedisStore 对象,并将 pass 选项设置为 redis.conf 中的密码。
选项数组设置如下:
应用程序配置中的行更改为如下所示:
我希望这对遇到此问题的任何人都有用。
My problem was caused by configuring my app with a RedisStore object to handle sessions. After enabling the password in the redis.conf file, the RedisStore object needed to be created with the pass option set to the password in redis.conf.
The options array is set like this:
and the line in the app configuration is changed to look like this:
I hope this is useful to anybody who runs into this problem.