From 69482e4204fc384e5ad00872e881bf9d63d025cd Mon Sep 17 00:00:00 2001 From: "steven.liatti" <steven.liatti@hesge.ch> Date: Fri, 15 Feb 2019 09:35:50 +0100 Subject: [PATCH] Add simple and ugly condition to run application in case of missing MongoDB --- server/server.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/server/server.js b/server/server.js index abc5e44..086923e 100644 --- a/server/server.js +++ b/server/server.js @@ -17,6 +17,13 @@ logger.level = 'debug'; // Define the MongoDB connection const db = require('monk')(process.env.DB); +let useDb = false; +db.then(() => { + logger.info('Connection to MongoDB OK'); + useDb = true; +}).catch((err) => { + logger.error('Connection to MongoDB failed'); +}); const collection = db.get('kittens'); // For simple purpose, laxist CORS rules @@ -106,8 +113,11 @@ app.get('/swagger.json', (req, res) => { app.get('/:width/:height', async (req, res) => { logger.debug(req); try { - // If a matching doc is found, return it - const doc = await collection.findOne({ width: req.params.width, height: req.params.height }); + let doc = undefined; + if (useDb) { + // If a matching doc is found, return it + doc = await collection.findOne({ width: req.params.width, height: req.params.height }); + } if (doc) { doc._id = undefined; res.status(200).send(doc); @@ -123,8 +133,10 @@ app.get('/:width/:height', async (req, res) => { width: req.params.width, height: req.params.height } - - collection.insert(kitten); + if (useDb) { + logger.debug('insert in DB', kitten); + collection.insert(kitten); + } res.status(200).send(kitten); } } catch (error) { -- GitLab