From 8bf2174b861225839aaab8427ce37f0461ff33fc Mon Sep 17 00:00:00 2001 From: Chris Dias Date: Sat, 12 Nov 2016 11:54:42 -0800 Subject: [PATCH] make connection to mongo more robust --- server.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index ba5e71a5d..7b7e0ec8a 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,12 @@ var bodyParser = require('body-parser'); var methodOverride = require('method-override'); // configuration =============================================================== -mongoose.connect(database.localUrl); // Connect to local MongoDB instance. A remoteUrl is also available (modulus.io) +// Connect to local MongoDB instance using environment variable if exists, a remoteUrl is also available (modulus.io) +mongoose.connect(process.env.MONGODB || database.localUrl); + +// handle connection failure +var db = mongoose.connection; +db.on('error', console.error.bind('error: unable to connect to database!')); app.use(express.static('./public')); // set the static files location /public/img will be /img for users app.use(morgan('dev')); // log every request to the console