diff --git a/server/db-mongodb.js b/server/db-mongodb.js index b0d62cd..a29ad0f 100644 --- a/server/db-mongodb.js +++ b/server/db-mongodb.js @@ -13,7 +13,14 @@ export function generateUUID() { }); } -const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'; +function getMongoUri() { + const envUri = (process.env.MONGODB_URI || '').trim(); + if (envUri.startsWith('mongodb://') || envUri.startsWith('mongodb+srv://')) { + return envUri; + } + return 'mongodb://127.0.0.1:27017'; +} + const DB_NAME = process.env.DB_NAME || 'hospital'; let client; @@ -62,7 +69,8 @@ const memStore = { export async function initDb() { try { - client = new MongoClient(MONGODB_URI, { + const uri = getMongoUri(); + client = new MongoClient(uri, { connectTimeoutMS: 5000, serverSelectionTimeoutMS: 5000 });