使用nodejs app配置redis

时间:2015-08-17 11:33:46

标签: javascript node.js heroku

问题是我有一个简单的应用程序,我希望部署到heroku我遇到的问题是让redisToGo在myoku上使用我的应用程序。任何帮助将不胜感激。

这是我的app.js

var conf = {
    port: 8888,
    debug: false,
    dbPort: 6379,
    dbHost: '127.0.0.1',
    dbOptions: {},
    mainroom: 'MainRoom'
};


var express = require('express'),
    http = require('http'),
    socketio = require('socket.io'),
    events = require('events'),
    _ = require('underscore'),
    redis = require('redis'),
    sanitize = require('validator').sanitize;

var app = express(),
    server = http.createServer(app),
    io = socketio.listen(server);
    server.listen(conf.port);

app.configure(function() {
    app.use(express.bodyParser());
    app.use(express.static(__dirname + '/static'));
 });

 var RedisStore = require('socket.io/lib/stores/redis'),
    pub = redis.createClient(conf.dbPort, conf.dbHost, conf.dbOptions),
    sub = redis.createClient(conf.dbPort, conf.dbHost, conf.dbOptions),
    db = redis.createClient(conf.dbPort, conf.dbHost, conf.dbOptions);
 io.set('store', new RedisStore({
    redisPub: pub,
    redisSub: sub,
    redisClient: db
 }));
 io.set('log level', 1);

我试过这样做但没有运气.....

var conf = {
    port: 8888,
    debug: false,
    dbPort: 9685,
    dbHost: 'redis://redistogo:<pass>@sole.redistogo.com:9685/',
    dbOptions: {},
    mainroom: 'MainRoom'
};

我一直收到此错误

 Error: Redis connection to redis://redistogo:<pass>@sole.redistogo.com:9685/ failed - connect ENOENT

我也尝试了这个并让它连接但现在它抱怨错误:错误:需要NOAUTH身份验证。

var conf = {
   port: 8888,
   debug: false,
   dbPort: 9685,
   db: 'xxxxxxxx',
   dbPass: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
   dbHost: 'xxxx.redistogo.com',
   dbOptions: {no_ready_check: true},
   mainroom: 'MainRoom'
};

// Socket.io store configuration
var RedisStore = require('socket.io/lib/stores/redis'),
    pub = redis.createClient(conf.dbPort, conf.dbHost, conf.dbOptions, conf.dbPass),
    sub = redis.createClient(conf.dbPort, conf.dbHost, conf.dbOptions, conf.dbPass),
    db = redis.createClient(conf.dbPort, conf.dbHost, conf.dbOptions, conf.dbPass);
 io.set('store', new RedisStore({
    redisPub: pub,
    redisSub: sub,
    redisClient: db
 }));

1 个答案:

答案 0 :(得分:1)

使用node-redis客户端时,您需要使用auth_pass上的dbOptions属性声明redis密码:

var conf = {
   port: 8888,
   debug: false,
   dbPort: 9685,
   db: 'xxxxxxxx',
   dbHost: 'xxxx.redistogo.com',
   dbOptions: {
     auth_pass: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
     no_ready_check: true,
   },
   mainroom: 'MainRoom'
};

var db = redis.createClient(
  conf.dbPort,
  conf.dbHost,
  conf.dbOptions
);

如果您查看API,可以看到redis.createClient不接受密码属性:

  

redis.createClient(端口,主机选项)

您正在寻找auth_pass选项:

  

auth_pass默认为null。默认情况下,客户端将尝试连接   没有认证。如果设置,客户端将在连接上运行redis auth命令。

https://github.com/NodeRedis/node_redis/blob/master/README.md#overloading