Skip to content

Instantly share code, notes, and snippets.

@kelegorm
Last active August 29, 2015 14:02
Show Gist options
  • Save kelegorm/aaf0e7a45161ae53b011 to your computer and use it in GitHub Desktop.
Save kelegorm/aaf0e7a45161ae53b011 to your computer and use it in GitHub Desktop.
Getting AppFog mongodb service settings
//... your project imports and mongoose
var mongoose = require('mongoose');
//... your application code
// start mongoose:
mongoose.connect(getMongodbURL(), function(error) {
// your initial code like bellow
if (error) {
console.log('Mongoose.connect error: ' + error);
return;
}
app.listen(process.env.PORT || port);
console.log('Listening on port ' + port);
}
function getMongodbURL() {
// trying to get mongodb parameters from appfog environment
if (process.env.VCAP_SERVICES) {
var mongo = JSON.parse(process.env.VCAP_SERVICES)['mongodb-1.8'][0]['credentials'];
}
// it is your localhost settings
if (!mongo) {
mongo = {
"hostname":"localhost",
"port":27017,
"db":"motivator"
}
}
var result = 'mongodb://';
if (mongo.username && mongo.password) {
result += mongo.username + ":" + mongo.password + "@";
}
result += mongo.hostname + ":" + mongo.port + '/' + mongo.db;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment