Wednesday, June 20, 2018

Node.js redis over SSL Connection Examples

var redis = require('redis');
var tls = require('tls');
var fs = require('fs');

var ssl = {
key: fs.readFileSync("private.key",encoding='ascii'),
cert: fs.readFileSync("user.crt",encoding='ascii'),
ca: [ fs.readFileSync("ca.pem",encoding='ascii') ],
rejectUnauthorized: false
};
var options = {
tls: ssl,
password:'xxxxxx'
};

var client = redis.createClient([the redis port], '[The redis url]', options);

try {
client.on('connect', function() {
console.log('Connected to Redis');
});
}
catch (e) {
console.log('1' + e)
}
console.log('here')

try{
client.set("foo", "bar", redis.print);
}
catch (e){
console.log('2' + e)
}
try{
client.get("foo", function (err, reply) {
if (err) throw err;
console.log(reply.toString());
});}
catch (e) {
console.log('3' + e)
}

try{
client.on('reconnecting', function() {
console.log('Connected to Redis');
});
}
catch (e) {
console.log('4' + e)
}

No comments:

Post a Comment