I have an app with MongoDB as backend. When the app is started, I set up the connection and use it later on requests.
But if in the mean time my db conncetion fails (ie. mongod crashes), how can I check that on the request time?
To clarify a bit:
- currently I have an "api.js", which does
db.once('open', function../* setup */)
- on request, I do
db.find(conditions, function(err, res) { if (err) ...else ...})
.
What I want to do is check if the connection is alive before the db.find()
clause. So if it is down, I can try to restart the db connection.
P.S. I know, I should probably set some sort of a connection pool or similar instead of keeping the connection alive all the time, but right now it's set as it is.