Created
July 9, 2012 17:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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