In DegitalOcean, i have to manage two servers 'Nodeserver' and a redis server 'Redisserver' by making the connection possible from a node/express application on the former for using the latter's database.
For the purpose of local tests, i've also installed another redis server 'LocalRedisOnNodeServer' locally on 'Nodeserver'.
I have configured the 'Redisserver' to accept external connections as in https://www.digitalocean.com/community/tutorials/how-to-set-up-a-redis-server-as-a-session-handler-for-php-on-ubuntu-14-04 and when trying to connect from the 'LocalRedisOnNodeServer's redis-cli i was able to access the remote 'Redis_server' database and reading and writting operations were also possible through it.
I still didn't configure any security's mesures for 'Redis_server' (as to edit iptables..) and i've only set a password (requirepass in the redis.conf) and i still don't know if a more secured authentification can be made with SSH on this server (or to configure my application code for it too).
The following is the app.js file which contains the express's session and the RedisStore (receiving a redis client as args) passed to the session.
var express = require('express');
var app = express();
var routes = require('./routes');
var errorHandlers = require('./middleware/errorhandlers');
var log = require('./middleware/log');
var partials = require('express-partials');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
var bodyParser = require('body-parser');
var redis = require('redis');
var config = require('./config');
app.set('view engine', 'ejs');
app.set('view options', {defaultLayout: 'layout'});
app.use(partials());
app.use(log.logger);
app.use(express.static(__dirname + '/static'));
app.use(cookieParser(config.secret));
console.log(config.redisConf);
var redisClient = redis.createClient(config.redisConf);
redisClient.on('connect', function() {
console.log('connected to redis!!');
});
redisClient.set('framework', 'AngularJS', function(err, reply) {
console.log('the framwork var was SET to AngularJS : the following is the server answer : ');
console.log(reply);
});
app.use(session({
secret: config.secret,
resave: false,
saveUninitialized: true,
store: new RedisStore({client: redisClient})
}));
// right after the session
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', routes.index);
app.get('/login', routes.login);
app.post('/login', routes.loginProcess);
app.get('/chat', routes.chat);
app.get('/account/login', routes.login);
app.get('/error', function(req, res, next){
next(new Error('A contrived error'));
});
app.use(errorHandlers.error);
app.use(errorHandlers.notFound);
app.listen(config.port);
console.log("App server running on port " + config.port);
and this is the config.js :
var config = {
port: 3000,
secret: 'secret',
redisConf: {
host: 'xxx.xxx.xxx.xxx.', // The redis's server ip
port: '6379',
pass: 'theredispass'
}
};
module.exports = config;
I've tested the app connection to the local redis server 'LocalRedisOnNodeServer' and and it was successful as well as reading and writing keys (for the set and get..). which is a proof that the app code is fine. But when i changed from the redis host (redisConf.host ) from the local 127.0.0.1 to the 'Redisserver' ip, only the connection occurs (the redisClient.on('connect' callback logs that it's 'connected to redis!!' but reading and writting functions(get and set) fail as their callbacks haven't been triggered and the other problem was that the express's session couldn't be created and its value remains to undefined.
I wonder why the connection as well as reading and writting operations were all possible within the local redis server's client on the 'Node_server's shell (the redis-cli) to the remote server while the redisClient in application code failed.
No errors were mentioned in redis_6379.log
Best regards
I solved it first by adding
client.on("error", function (err) {
console.log("Error " + err);
});
so i could see what was wrong (indeed my mistake was to think that the redisClient belongs to the connect-redis module so i didn't see all these available options on the node_redis module..) and that added code showed me that the problem was like in this thread and thanks to this answer and also, i saw that it was better to move the get methode inside the set's callback as the remote server would answer asynchronuesly..