I have created a Nodejs server that uses Redis. It runs fine when I am running it in my machine. But I get the error above when I try to push the code to heroku. I already installed Redis togo and I can see the REDISTOGO_URL
in my config variable.
Following some Stackoverflow posts I did:
let RedisStore = require('connect-redis')(session)
let redisClient = redis.createClient()
if(process.env.REDISCLOUD_URL){
let redisURL = url.parse(process.env.REDISCLOUD_URL);
redisClient = redis.createClient(redisURL)
}
My app runs fine in development but when I try to start my app in heroku I get the following error when I do heroku logs --tail
:
Error: Redis connection to 127.0.0.1:6379 failed – connect
ECONNREFUSED 127.0.0.1:6379
3
Answers
Your code seems to be expecting a
REDISCLOUD_URL
environment variable. If you’ve definedREDISTOGO_URL
then that would explain why it isn’t working.you got that error is because you first try to create redis client with
redis.createClient()
. by default, it will connect to127.0.0.1:6379
.It works fine on localhost because it won’t go into the
if (process.env.REDISCLOUD_URL
and the local redis is available.To fix this,
Heroku requires you to provide a REDISCLOUD_URL environment variable when creating redisClient.
so you should provide the REDISCLOUD_URL while creating the Redis client.