开发者

Error connecting express node.js app to redis server when using requirepass in redis.conf

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> 开发者_StackOverflow中文版(/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.


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:

    var redisStoreOpts = {'pass':REDIS_PASS};

and the line in the app configuration is changed to look like this:

    app.use(express.session({ secret: SESSION_SECRET, store: new RedisStore(redisStoreOpts) }));

I hope this is useful to anybody who runs into this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜