Reputation: 407
Preamble: I'm running a node app on AWS EB and I'm using Elasticache redis server for session management. I'm trying to connect to elasticache using connect-redis and it doesn't seem to be using the host setting that I provide, instead defaulting to 127.0.0.1.
I'm running a node app with a redis server set up for cache. main.js below:
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');
var redis = require("redis");
var redisStore = require('connect-redis')(expressSession);
// Application libraries
var express = require('express');
// Redis client
var redisclient = redis.createClient();
redisclient.on("error", function (err)
{
console.log("REDIS Error " + err);
});
// Initialise the application
var app = express();
var sessionport = isNaN(process.env.SESSION_PORT) ? 6379 : Number(process.env.SESSION_PORT);
console.log('redis://' + process.env.SESSION_HOST + ':' + sessionport);
var store = new redisStore({
host: process.env.SESSION_HOST,
port: sessionport,
client: redisclient,
ttl : 2.29e+7
});
app.use(expressSession({
saveUninitialized: false,
resave: false,
secret: process.env.SESSION_SECRET,
store: store
}));
My environment variables are:
SESSION_HOST: my-session-host.usw2.cache.amazonaws.com
SESSION_PORT: 6379
SESSION_SECRET: supersecret
However, when my server starts up I'm seeing the following output from the node output:
Worker #1 is online. (pid 17643)
redis://liamegan-session-001.i0o002.0001.usw2.cache.amazonaws.com:6379
REDIS Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
REDIS Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
REDIS Error Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
It looks like it's trying to connect to 127.0.0.1 despite my providing it with a host. I've also tried constructing and providing a redis URL to the redisStore constructor.
Any I doing something obviously wrong here?
Upvotes: 3
Views: 2214
Reputation: 407
The problem here was that I was supplying the existing redisclient to the redisStore constructor. Doing this trumps the creation of a new client and therefore overrides the host and port set there. To fix this, I simply added the host and port to redis.createClient.
// Redis client
var sessionport = isNaN(process.env.SESSION_PORT) ? 6379 : Number(process.env.SESSION_PORT);
var redisclient = redis.createClient({
host: process.env.SESSION_HOST,
port: sessionport
});
Upvotes: 4