Skip to content

Add MongoDB reconnection logic on connection drop 🔄 #523

Description

@anoopcodehack

Add MongoDB reconnection logic on connection drop 🔄

File: server/index.js

If MongoDB disconnects the server
just crashes silently 💀 fr!!

Fix: add reconnection handler:
mongoose.connection.on('disconnected', () => {
console.log('⚠️ MongoDB disconnected!
Attempting to reconnect...')
setTimeout(() => {
mongoose.connect(process.env.MONGO_URI)
}, 5000)
})

mongoose.connection.on('reconnected', () => {
console.log('✅ MongoDB reconnected!')
})

mongoose.connection.on('error', (err) => {
console.error('❌ MongoDB error:', err)
})

~12 lines! No frontend changes needed 🎯
Makes backend production ready!! 🔥

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions