Connecting our project

Now we know what we're doing, let's connect our project to a database using the default Mongoose connection.

Creating the connection

For the sake of well organized code, let's create a folder called model, and in that, an empty JavaScript file called db.js. We'll use this for managing the Mongoose connection, and will add to it in later chapters.

At this stage the file needs to do three things:

  1. Bring in the Mongoose module
  2. Build the connection string for the database
  3. Open the Mongoose connection to the database

So in your /model/db.js file, enter the following:

// Bring Mongoose into the project
var mongoose = require( 'mongoose' );

// Build the connection string
var dbURI = 'mongodb://localhost/MongoosePM';

// Create the database connection
mongoose.connect(dbURI);

Each of the three objectives is achieved with just one line of code—pretty simple don't you think!

Catching the events

Next up we want to set up our event handlers. At this stage, we are just going to log messages to the console, but they are useful containers, and important to understand. We will also catch when the Node process is ending and close the Mongoose connection.

So, still in db.js, after the connection code add the following snippets:

mongoose.connection.on('connected', function () {
  console.log('Mongoose connected to ' + dbURI);
});

mongoose.connection.on('error',function (err) {
  console.log('Mongoose connection error: ' + err);
});

mongoose.connection.on('disconnected', function () {
  console.log('Mongoose disconnected'),
});

process.on('SIGINT', function() {
  mongoose.connection.close(function () {
    console.log('Mongoose disconnected through app termination'),
    process.exit(0);
  });
});

Opening the connection at application start

Now we need to tell the project to use this file, so that we can connect to the database when the application starts. This is as simple as requiring our new file in app.js—you may remember that this is the file we run to start the application.

At the top of your app.js file you should see some of the default modules being required like in the following:

var express = require('express')
  , routes = require('./routes')

We are simply going to add one line in here, to require our model/db.js file immediately after express is required. You should end up with something resembling this following code:

var express = require('express')
  , db = require('./model/db')
  , routes = require('./routes')

Creating the database

You will notice that we haven't actually created a MongoDB database yet. The good—and perhaps surprizing—news is that we don't have to. Nor do we have to explicitly create any collections.

MongoDB will create a database the first time anything is saved to it, and the same for collections.

I love this approach. This is one of the aspects that I find really speeds up application development and prototyping.

You can of course connect to an existing database, if you already have one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.116.20