From 045c53accb309f71cc89953726b448a498ed6d8b Mon Sep 17 00:00:00 2001 From: tenthirtyone Date: Fri, 4 Aug 2017 10:53:00 -0400 Subject: [PATCH] Handling Mongo connection errors and starting bcoin once the db connects --- config/index.js | 2 ++ index.js | 4 +++- lib/db/index.js | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/index.js b/config/index.js index 8980e3e..2db41d2 100644 --- a/config/index.js +++ b/config/index.js @@ -12,6 +12,8 @@ const config = { uri: 'mongodb://localhost/bitcore', options: { useMongoClient: true, + socketTimeoutMS: 0, + connectTimeoutMS: 0 }, }, api: { diff --git a/index.js b/index.js index d47d732..3e5c83c 100644 --- a/index.js +++ b/index.js @@ -14,4 +14,6 @@ Api.listen(config.api.port, () => { 'listening on port 3000'); }); -node.start(); +db.connection.once('open', function() { + node.start(); +}); diff --git a/lib/db/index.js b/lib/db/index.js index e6cb4c9..43f325f 100644 --- a/lib/db/index.js +++ b/lib/db/index.js @@ -1,3 +1,10 @@ const mongoose = require('mongoose'); +const logger = require('../logger'); + +mongoose.connection.on('error', (err) => { + logger.log('error', + `Failed to connect to Mongo Database + ${err}`); +}); module.exports = mongoose;