From edd71f1ab561cdf29cc5ef94a611d701c192486a Mon Sep 17 00:00:00 2001 From: hamza ben younes <89034867+Hamzabny@users.noreply.github.com> Date: Mon, 11 Nov 2024 07:38:10 +0100 Subject: [PATCH] Update app.js add --- app.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app.js b/app.js index 7ae6e5d4..9a16b5d8 100644 --- a/app.js +++ b/app.js @@ -15,11 +15,21 @@ async function getApp() { // Use AZURE_COSMOS_CONNECTIONSTRING if available, otherwise fall back to MONGODB_URI const mongoUri = process.env.AZURE_COSMOS_CONNECTIONSTRING || process.env.MONGODB_URI; - mongoose.connect(mongoUri).then(() => { + const options = { + useNewUrlParser: true, + useUnifiedTopology: true, + serverSelectionTimeoutMS: 30000, // Increase timeout duration + socketTimeoutMS: 45000, // Increase socket timeout + }; + + try { + // Wait for the database connection to succeed + await mongoose.connect(mongoUri, options); console.log('Connected to database'); - }).catch((err) => { + } catch (err) { console.error('Error connecting to database:', err); - }); + throw new Error('Database connection failed'); // Will stop app startup if failed + } var app = express();