I just wanted to know, at the beginning of my NodeJS process, if Redis is started or not (so users session will be stored or not).
Here is what I have for the moment :
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var redis = require("redis");
var client = redis.createClient(global.redis.host, global.redis.port);
// Check if redis is running
var redisIsReady = false;
client.on('error', function(err) {
redisIsReady = false;
console.log('redis is not running');
console.log(err);
});
client.on('ready', function() {
redisIsReady = true;
console.log('redis is running');
});
// Here I use express-session, but I want to set a store only if redis is ready
sessOptions = {
[...]
}
// Store setting
if (redisIsReady) {
sessOptions.store = new RedisStore({
host: global.redis.host,
port: global.redis.port
});
} else {
console.log("redis is not running - sessions won't be written to disk");
}
// app.use(session(sessOptions))
Here is what it's output in both cases (when Redis is running or not) :
redis is not running - sessions won't be written to disk
redis is not running
Error: Redis connection to 6379:localhost failed - connect ENOENT
So I have 2 questions :
How can I do to check if redis is running before setting my SessionStore (is there anyway to check synchronously if redis is running) ?
Why does it giving me an error even when redis is running ?
THANKS!
Note : I'm using default host/port (localhost, 6379) and the RedisStore works as expected.
Note 2 : I'm using Windows... but don't be affraid, it should have the same behavior!
Update : Question #2 answered by vmx => Thanks!
Update 2 : Question #1 answered by Nathan => Thanks!