I'm using express
and connect-redis
to get the server to use Redis for sessions.
import Session from 'express-session';
const session = Session({
resave: true,
saveUninitialized: true,
key: '...',
secret: '...',
store: initializeRedis(Session);
});
The redis initializer looks like the following:
import { createClient as createRedisClient } from 'redis';
import connectRedis from 'connect-redis';
export default function initializeRedis(Session) {
const redisClient = createRedisClient();
const RedisStore = connectRedis(Session);
return new RedisStore({
host: 'redis',
port: 6378,
});
}
According to the previous option parameters, the server should be connecting to redis
instead of the default 127.0.0.1
through port 6378
instead of the default 6379
However, regardless of what these values equal to I always get the same output:
Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Is there anything I missed out during the configuration?
The Redis client was missing the host
and port
parameters. There was no client inside the RedisStore
.
const redisClient = createRedisClient({
host: 'redis',
port: 6379
});
return new RedisStore({
client: redisClient,
});
Full copy-paste'able code at the end of the post, in case it is useful for anyone else.
There were a couple ways to solve the code above.
1. The const redisClient = createRedisClient()
was assigned nowhere, but it was still doing its connecting job. Hence, the server was crashing because it could not connect, but it was not a RedisStore
fault.
At the time I posted the question, I didn't notice that line was there and I thought it was crashing due to the store.
In fact, without that line it works, because the RedisStore has a default client implementation.
or created for you using the host, port, or socket params.
return new RedisStore({
host: 'redis',
port: 6379,
});
2. The redis client was missing the host and port custom values (default values are '127.0.0.1'
and 6379
)
import { createClient as createRedisClient } from 'redis';
const redisClient = createRedisClient({
host: 'redis',
port: 6379
});
If we are using a specific client implementation, it also makes sense that we pass it into the RedisStore
:
const RedisStore = connectRedis(Session);
return new RedisStore({
client: redisClient,
});
server.js
import Session from 'express-session';
import initializeRedis from './redis';
// Initialize session.
const session = Session({
resave: true,
saveUninitialized: true,
key: 'SID',
secret: 'Luke Skywalker',
store: initializeRedis(Session)
});
app.use(session);
redis.js
import { createClient as createRedisClient } from 'redis';
import connectRedis from 'connect-redis';
export default function initializeRedis(Session) {
const redisClient = createRedisClient({
host: 'redis', // or '127.0.0.1'.
port: 6379,
});
const RedisStore = connectRedis(Session);
return new RedisStore({ client: redisClient });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With