Last active
August 29, 2015 14:02
-
-
Save kelegorm/aaf0e7a45161ae53b011 to your computer and use it in GitHub Desktop.
Getting AppFog mongodb service settings
This file contains hidden or 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
//... 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