How to make a Mongoose connection fail gracefully

When my Node app starts, it tries to connect to the database like so:

var db = mongoose.connect('mongodb://what:ever.com:1234/database', function(err) {
    if(err) console.log('MongoDB: connection error -> ' + err);
    else console.log('MongoDB: successfully connected');
});

When it fails, I get an error exactly as I expect, but my Node app terminates. How can I let the node app continue running when the connection returns an error?

Can someone point me in the direction of Mongoose best practices for long-running applications? I know Mongoose has a retry connection flag set true by default, but what else should I be doing?

Hey funny you should have asked this question. I wast just writing this a few minutes ago. I'm using a coffeescript variant but heres how it looks. I'm also using colors.js for nice colored feedback on the console but other than that this should catch your db error nicely without dying out.

Let me preface by saying I'm no expert, I just started picking this up a few weeks ago.

mongoose = require "mongoose"
db_address = "localhost/nodesample-dev"

mongoose.connection.on "open", (ref) ->
  console.log "Connected to mongo server!".green

mongoose.connection.on "error", (err) ->
  console.log "Could not connect to mongo server!".yellow
  console.log err.message.red

try
  mongoose.connect("mongodb://#{db_address}")
  db = mongoose.connection
  console.log "Started connection on " + "mongodb://#{db_address}".cyan + ", waiting for it to open...".grey

catch err
  console.log "Setting up failed to connect to #{db_address}".red, err.message

Here it is in compiled JS:

var db, db_address, mongoose;
mongoose = require("mongoose");
db_address = "localhost/nodesample-dev";

mongoose.connection.on("open", function(ref) {
  return console.log("Connected to mongo server!".green);
});

mongoose.connection.on("error", function(err) {
  console.log("Could not connect to mongo server!".yellow);
  return console.log(err.message.red);
});

try {
  mongoose.connect("mongodb://" + db_address);
  db = mongoose.connection;
  console.log("Started connection on " + ("mongodb://" + db_address).cyan + ", waiting for it to open...".grey);
} catch (err) {
  console.log(("Setting up failed to connect to " + db_address).red, err.message);
}