From 831b36a1e12efa0b29a7ca1c76e02d2ff9615d9b Mon Sep 17 00:00:00 2001 From: zeim839 Date: Wed, 1 May 2024 11:03:40 +0400 Subject: [PATCH] fix: wait for DB to connect before serving --- app.js | 9 +++++---- utils/cache.js | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index 67df7d2..835e288 100644 --- a/app.js +++ b/app.js @@ -21,8 +21,6 @@ const http = require('http').createServer(app) const apiRoute = require('./routes/api') const cache = new CacheModule() -cache.register(...callbacks) -cache.start(config.cache_interval) app.set('view engine', 'ejs') app.use(compression()) @@ -81,8 +79,11 @@ app.get('/*', (req, res) => { res.render('404', { version: config.VERSION }) }) -db.connect() -http.listen(config.port, () => console.log(`Server running on port ${config.port}`)) +db.connect().then(async () => { + await cache.register(...callbacks) + cache.start(config.cache_interval) + http.listen(config.port, () => console.log(`Server running on port ${config.port}`)) +}) const stop = () => { http.close() diff --git a/utils/cache.js b/utils/cache.js index 7c13c83..8f9b546 100644 --- a/utils/cache.js +++ b/utils/cache.js @@ -30,7 +30,9 @@ class Cache { } start (interval) { - if (typeof (interval) !== 'number') { return new Error('Expected interval to be of type number') } + if (typeof (interval) !== 'number') { + return new Error('Expected interval to be of type number') + } // Run AT MOST once every 'interval' milliseconds this.process = setInterval(() => {