Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Wcc723 committed Oct 4, 2023
1 parent 63b2e70 commit 867ccc1
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,24 @@ const connectionString =
process.env.MONGODB_URI ||
`mongodb://${username}:${password}@localhost:27017/${dbName}?authMechanism=${authMechanism}`;

mongoose
.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

async function connectWithRetry() {
try {
await mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB 已經連線');
})
.catch((err) => console.log(err));
} catch (err) {
console.log('MongoDB 連線錯誤, 五秒後重試...', err);
await sleep(5000);
return connectWithRetry();
}
console.log('MongoDB connected successfully');
}

connectWithRetry();
// MongoDB 連線 End

var app = express();
Expand Down

0 comments on commit 867ccc1

Please sign in to comment.