- URI Syntax - The Uniform Resource Locator (URI) syntax for a MongoDB is as follows:
- db.js Updates - Add the following (or similar depending on DB name) to your db.js file:
var dbURI = 'mongodb://localhost/blogger';
mongoose.connect(dbURI);
Note: - The first time your application successfully connects to the database the database will
be created, if needed.
- Monitoring the Mongoose Connection - You can add additional code to the bottom of db.js
in order for your app to write to the console when connection events happen:
// Monitor and report when database is connected
mongoose.connection.on('connected', function () {
console.log('Mongoose connected to ' + dbURI);
});
// Monitor and report error connecting to database
mongoose.connection.on('error',function (err) {
console.log('Mongoose connection error: ' + err);
});
// Monitor and report when database is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose disconnected');
});
- Graceful Shutdown and DB Disconnect - You will need extra code in db.js in order
for your application to gracefully shutdown and disconnect from the database when its process
terminates. Add the following to the bottom:
// Closes (disconnects) from Mongoose DB upon shutdown
gracefulShutdown = function (msg, callback) {
mongoose.connection.close(function () {
console.log('Mongoose disconnected through ' + msg);
callback();
});
};
// For nodemon restarts
process.once('SIGUSR2', function () {
gracefulShutdown('nodemon restart', function () {
process.kill(process.pid, 'SIGUSR2');
}); });
// For app termination
process.on('SIGINT', function() {
gracefulShutdown('app termination', function () {
process.exit(0);
}); });
// For Heroku app termination
process.on('SIGTERM', function() {
gracefulShutdown('Heroku app shutdown', function () {
process.exit(0);
}); });
- A Complete db.js File - Here is the complete file:
var mongoose = require( 'mongoose' );
var gracefulShutdown;
var dbURI = 'mongodb://localhost/myapp';
mongoose.connect(dbURI);
// Monitor and report when database is connected
mongoose.connection.on('connected', function () {
console.log('Mongoose connected to ' + dbURI);
});
// Monitor and report error connecting to database
mongoose.connection.on('error',function (err) {
console.log('Mongoose connection error: ' + err);
});
// Monitor and report when database is disconnected
mongoose.connection.on('disconnected', function () {
console.log('Mongoose disconnected');
});
// Closes (disconnects) from Mongoose DB upon shutdown
gracefulShutdown = function (msg, callback) {
mongoose.connection.close(function () {
console.log('Mongoose disconnected through ' + msg);
callback();
});
};
// For nodemon restarts
process.once('SIGUSR2', function () {
gracefulShutdown('nodemon restart', function () {
process.kill(process.pid, 'SIGUSR2');
}); });
// For app termination
process.on('SIGINT', function() {
gracefulShutdown('app termination', function () {
process.exit(0);
}); });
// For Heroku app termination
process.on('SIGTERM', function() {
gracefulShutdown('Heroku app shutdown', function () {
process.exit(0);
}); });