title: Mongoose connect method fails on simple Node Server. Express, Mongoose, Path
body: <p>This is the first time I'm running node with mongoose. I'm following some tutorials in this backbone book and I'm up to this chapter on creating a restful api using express, mongoose and I'm following the code exactly I've even come to copying and pasting but it's still not working. Here is the code:</p>
  
  <p><a href="http://addyosmani.github.io/backbone-fundamentals/#creating-the-back-end" rel="nofollow">http://addyosmani.github.io/backbone-fundamentals/#creating-the-back-end</a></p>
  
  <pre><code>// Module dependencies.
  var application_root = __dirname,
      express = require( 'express' ), //Web framework
      path = require( 'path' ), //Utilities for dealing with file paths
      mongoose = require( 'mongoose' ); //MongoDB integration
  
  //Create server
  var app = express();
  
  // Configure server
  app.configure( function() {
      //parses request body and populates request.body
      app.use( express.bodyParser() );
  
      //checks request.body for HTTP method overrides
      app.use( express.methodOverride() );
  
      //perform route lookup based on url and HTTP method
      app.use( app.router );
  
      //Where to serve static content
      app.use( express.static( path.join( application_root, 'site') ) );
  
      //Show all errors in development
      app.use( express.errorHandler({ dumpExceptions: true, showStack: true }));
  });
  
  //Start server
  var port = 4711;
  app.listen( port, function() {
      console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
  });
  
  // Routes
  app.get( '/api', function( request, response ) {
      response.send( 'Library API is running' );
  });
  
  //Connect to database
  mongoose.connect( 'mongodb://localhost/library_database');
  
  //Schemas
  var Book = new mongoose.Schema({
      title: String,
      author: String,
      releaseDate: Date
  });
  
  //Models
  var BookModel = mongoose.model( 'Book', Book );
  </code></pre>
  
  <p>I've been poking around on stack overflow and other sites trying to resolve the issue but nothing I found seemed to allow me to connect to the mongodb.</p>
  
  <p><strong>The first error is:</strong></p>
  
  <pre><code>events.js:72
          throw er; // Unhandled 'error' event
                ^
  Error: failed to connect to [localhost:27017]
      at null.&lt;anonymous&gt; (/Users/jeff/Sites/backbone-ex2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:536:74)
      at EventEmitter.emit (events.js:106:17)
      at null.&lt;anonymous&gt; (/Users/jeff/Sites/backbone-ex2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15)
      at EventEmitter.emit (events.js:98:17)
      at Socket.&lt;anonymous&gt; (/Users/jeff/Sites/backbone-ex2/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10)
      at Socket.EventEmitter.emit (events.js:95:17)
      at net.js:411:14
      at process._tickCallback (node.js:415:13)
  </code></pre>
  
  <p><strong>This change seems to fix that issue:</strong></p>
  
  <pre><code>    mongoose.connect( 'mongodb://localhost/library_database', function(err) { if (err) console.log(err); } );
  </code></pre>
  
  <p><strong>After that express works but mongodb fails to connect:</strong></p>
  
  <pre><code>Express server listening on port 4711 in development mode
  [Error: failed to connect to [localhost:27017]]
  </code></pre>
  
  <p><strong>I tried changing to this:</strong></p>
  
  <pre><code>mongoose = require( 'mongoose' ).Mongoose;
  </code></pre>
  
  <p>I also tried running <strong>mongod</strong> in the cli with some variations on options in the cli but that seems to just bring up the help page. I'm totally stuck... Any help would be greatly appreciated. Thanks in advance.</p>