I want to be able to send a friendly error message to my user whenever I cannot connect to my MongoDB server from inside node.js, instead of just having node.js freaking out as it does right now.
Apparently this won’t work as I cannot access the response variable from outside a route method (app.get('/' ...), so what should I do instead?
No need to display something fancy, but just informing the user about a temporary problem would be amazing.
mongoose.connect('mongodb://localhost/test', function(err) {
if (err) {
//response.send('Temporary problem', 500);
}
});
// Routes
app.get('/', index.index);
//... And so on
You could use alternative routes if connection error occured, so it won’t be neccessary to perfom connection checks in every request handler.
You should however find a way to drop route
'*'when your connection will be up.