My code:
db = mongoose.connect(
config[MONGO_HOST_CONF],
config[MONGO_ACCOUNTS_DB_CONF],
config[MONGO_PORT_CONF]
}
);
db.connection.on('opening', function() {
console.log("connecting");
});
db.connection.on('open', function() {
console.log("connected");
});
db.connection.on('error', function() {
console.log("disconnected");
});
db.connection.on('close', function() {
console.log("disconnected");
});
When I start my webserver which then opens a connection to mongodb, I can see "connected" in logs.
But when I do a mongodb stop, I dont see "disconnected" in logs. Neither do I see "connecting".
I am trying to detect if for some reason mongodb went down. Then reconnect before bailing out.
What is wrong above?
opening isn't an event that is sent by Mongoose, try connecting instead.
Also, close is an event that will be called when the client is closing the connection; in the situation where the server has stopped (or crashed), it isn't sent. Try using the disconnected event for that.