nodejs crashing on losing connection to mongodb

Viewed 57

When i try to restart mongodb service from services.msc, nodejs server is crashing with this error: enter image description here OR enter image description here

My code looks like this

_client = await MongoClient.connect(this._config.connectionString, {
          useNewUrlParser: true,
          useUnifiedTopology: true,
          reconnectTries: 60,
          reconnectInterval: 1000,
          keepAlive: true,
          autoReconnect: true,
        });

What ever i do, im not able to catch this exception there fore my node server is crashing. Is there a way to catch this reliably? To answer a question, why i'm trying this scenario: Sometimes our deployed server is losing connection(to mongoDB) in the cloud and ending up crashing and kick starting new pod in aks.

I'm using mongodb driver, not mongoose.

1 Answers

You can pass autoReconnect : false as one of the options in configuration. Documentation link

Related