Skip to content

Instantly share code, notes, and snippets.

@clarkbw
Created July 9, 2012 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clarkbw/3077823 to your computer and use it in GitHub Desktop.
Save clarkbw/3077823 to your computer and use it in GitHub Desktop.
example of connecting to a redis server for node.js running in a vcap environment
// default to the local dev redis environment
var port = 6379;
var host = "127.0.0.1";
var password = null;
// Check if we're running in the hosted VCAP environment instead of the localhost dev
if (process.env.VCAP_SERVICES){
var srv = null, credentials = null;
try {
srv = JSON.parse(process.env.VCAP_SERVICES);
credentials = srv["redis-2.2"][0]["credentials"];
host = credentials.host;
port = credentials.port;
password = credentials.password;
} catch (e) {
console.log(e);
// check your `vmc logs appname` for the settings, srv records could have changed
console.log(JSON.stringify(srv));
}
}
var redis = require("redis"),
client = redis.createClient(port, host);
// if there is a password do the auth
if (password !== null){
client.auth(password);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment