diff --git a/db.js b/db.js index c3060a376a..03283a75cf 100644 --- a/db.js +++ b/db.js @@ -1,4 +1,5 @@ var mongoose = require('mongoose'); +var cfenv = require("cfenv"); var Schema = mongoose.Schema; var Todo = new Schema({ @@ -15,10 +16,21 @@ var User = new Schema({ mongoose.model('User', User); +// CloudFoundry env vars +var mongoCFUri = cfenv.getAppEnv().getServiceURL('goof-mongo'); +console.log(JSON.stringify(cfenv.getAppEnv())); + +// Default Mongo URI is local var mongoUri = 'mongodb://localhost/express-todo'; -if (process.env.MONGOLAB_URI) { + +// CloudFoundry Mongo URI +if (mongoCFUri) { + mongoUri = mongoCFUri; +} else if (process.env.MONGOLAB_URI) { + // Generic (plus Heroku) env var support mongoUri = process.env.MONGOLAB_URI; } +console.log("Using Mongo URI " + mongoUri); mongoose.connect(mongoUri); diff --git a/package.json b/package.json index 108878acee..087260e07a 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ }, "dependencies": { "body-parser": "1.9.0", + "cfenv": "^1.0.4", "cookie-parser": "1.3.3", "ejs": "1.0.0", "ejs-locals": "1.0.2",