Mongoose connect method fails on simple Node Server. Express, Mongoose, Path
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:
http://addyosmani.github.io/backbone-fundamentals/#creating-the-back-end
// 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 );
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.
The first error is:
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [localhost:27017]
at null.<anonymous>
(/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.<anonymous>
(/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.<anonymous>
(/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)
This change seems to fix that issue:
mongoose.connect( 'mongodb://localhost/library_database',
function(err) { if (err) console.log(err); } );
After that express works but mongodb fails to connect:
Express server listening on port 4711 in development mode
[Error: failed to connect to [localhost:27017]]
I tried changing to this:
mongoose = require( 'mongoose' ).Mongoose;
I also tried running mongod 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.
No comments:
Post a Comment