Handle lost connection to mongo db from nodejs
If have the mongodb server process running, run this example.After, if you stop the server process, you'll see Error...close
being displayed.All possible events for the "on" function can be found here
var MongoClient = require('mongodb').MongoClient;// Connect to the dbMongoClient.connect('mongodb://localhost:27017/exampleDb', function(err, db) { if(err) { return console.dir(err); } console.log('We are connected'); // db.close(); db.on('close', function () { console.log('Error...close'); });});
Check out this:https://thecodebarbarian.com/managing-connections-with-the-mongodb-node-driver.html
The big problem is the driver would buffer the queries forever and never return before connection recovery instead of failing them out, which could hang your program's execution if people not aware about this.