Heroku 上的节点和 Redis 应用程序错误

Node & Redis Application Error on Heroku

我是 Heroku 和 Node 的新手,所以请多多包涵。我将我的工作聊天应用程序(在本地主机上)部署到 Heroku,但无法打开 Heroku 应用程序。

谁能帮我解决这个问题?

我在 Whosebug 上查看了一些类似的问题并做了 heroku config:add REDISCLOUD_URL='redis_cloud_url'heroku config:add NODE_ENV='production' 但这些都没有用。


这是我的 heroku 日志。

2015-04-11T19:42:35.568690+00:00 heroku[api]: Deploy 0244dd7 by MYMAIL@gmail.com
2015-04-11T19:42:38.004491+00:00 heroku[web.1]: Starting process with command `node app.js`
2015-04-11T19:42:39.182617+00:00 app[web.1]: Detected 512 MB available memory, 512 MB limit per process (WEB_MEMORY)
2015-04-11T19:42:39.182639+00:00 app[web.1]: Recommending WEB_CONCURRENCY=1
2015-04-11T19:42:39.793252+00:00 app[web.1]: events.js:85
2015-04-11T19:42:39.793258+00:00 app[web.1]:       throw er; // Unhandled 'error' event
2015-04-11T19:42:39.793260+00:00 app[web.1]:             ^
2015-04-11T19:42:39.793262+00:00 app[web.1]: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED
2015-04-11T19:42:39.793264+00:00 app[web.1]:     at RedisClient.on_error (/app/node_modules/redis/index.js:196:24)
2015-04-11T19:42:39.793266+00:00 app[web.1]:     at Socket.<anonymous> (/app/node_modules/redis/index.js:106:14)
2015-04-11T19:42:39.793268+00:00 app[web.1]:     at Socket.emit (events.js:107:17)
2015-04-11T19:42:39.793270+00:00 app[web.1]:     at net.js:459:14
2015-04-11T19:42:39.793271+00:00 app[web.1]:     at process._tickCallback (node.js:355:11)
2015-04-11T19:42:40.546881+00:00 heroku[web.1]: State changed from starting to crashed

好吧,其实我知道问题所在,问题是你使用的是 redis 云,所以 redis 不是 运行 on 127.0.0.1

看看 Heroku 上的 the official documentation;你不能将 REDISCLOUD_URL 设置为 redis_cloud_url,你必须让 Heroku 将它设置为 你的帐户 的 REDISCLOUD_URL,你可以通过这样做在您的终端

heroku config:get REDISCLOUD_URL

应该return像这样;

http://rediscloud:password@hostname:port

如果它 return 没有这种格式,那么插件没有为您的应用程序正确配置/激活。

连接到 redis 的代码应该如下所示:

var redis = require('redis');
var url = require('url');
var redisURL = url.parse(process.env.REDISCLOUD_URL);
var client = redis.createClient(redisURL.port, redisURL.hostname, {no_ready_check: true});
client.auth(redisURL.auth.split(":")[1]);

我用的是 Heroku Redis,不是 Redis To Go,也有这样的问题。但是我通过更改 Redis 客户端的初始化方式解决了这个问题,之前我有以下代码:

const
  redisOptions = require('./redisOptions'),
  redis = require('redis'),
  redisClient = redis.createClient(redisOptions);

现在我从 redisOptions 中删除了 url 参数,然后将其添加到 createClient 方法,即:

const
  redisOptions = require('./redisOptions'),
  redis = require('redis'),
  redisClient = redis.createClient(REDIS_URL, redisOptions);

然后错误被修复了。真的不确定为什么它在我的本地环境中没有问题,但我需要在 Heroku 上进行此修复。