Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  131] [ 4]  / answers: 1 / hits: 25279  / 10 Years ago, sun, november 30, 2014, 12:00:00

I'm following this book addyosmani - backbone-fundamentals for creating a simple backbone.js app with node server and mongodb as backend.



As instructed, I've installed the latest versions of Node.js fromnodejs.org and mongodb from www.mongodb.org and ran mongodb by followed the instructions.






The package.json is as follows:



    {
name: backbone-library,
version: 0.0.1,
description: A simple library application using Backbone,
dependencies: {
express: ~3.1.0,
path: ~0.4.9,
mongoose: ~3.5.5,
body-parser: ~1.9.1
}
}


The tutorial then suggests to add the following to server.js file for connection to mongoDB:



//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 got confused with the following line:



mongoose.connect( 'mongodb://localhost/library_database' );


because the book doesn't mention anything regarding creating a database named library_database and I haven't created manually - Still if I run the following in server.js:



mongoose.createConnection( 'mongodb://localhost/library_database' );

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
// we wait till mongo is ready before letting the http handler query users:
db.once('open', function () {
console.log('Running');
//Schemas
var Book = new mongoose.Schema({
title: String,
author: String,
releaseDate: Date
});

//Models
var BookModel = mongoose.model('Book', Book);
});


It logs Running..! (how come because I didn't create any library_database..?)



But, If I further follow the book by adding a GET request handler as shown below:



    //Get a list of all books
app.get( '/api/books', function( request, response ) {
return BookModel.find( function( err, books ) {
if( !err ) {
return response.send( books );
} else {
return console.log( err );
}
});
});


and hit it with the following request:



jQuery.get( '/api/books/', function( data, textStatus, jqXHR ) {
console.log( 'Get response:' );
console.dir( data );
console.log( textStatus );
console.dir( jqXHR );
});


According to the tutorial I'm supposed to see the logs, Instead I get the following error:



GET http://localhost:4711/api/books/ net::ERR_EMPTY_RESPONSE


The DB is currently listening to default port with the default database test.



I tried use library_database in mongo shell, but still get the same response from the node server.



Do I need to manually create a db for the application..? If so, how can I create an instance for the app and connect to it from node server..?



Or does mongoose automatically creates such a db in the applications root folder (very unlikely to happen)..?



I'm pretty new to node.js, mongodb, mongoose etc so I might be missing some basic concepts. What am I missing..?


More From » node.js

 Answers
4

I just found the examples for connect and createConnection in the docs:



mongoose.connect('mongodb://user:pass@localhost:port/database');
//-----------------------------------------------^


The one in tutorial is as follows:



mongoose.connect( 'mongodb://localhost/library_database' );


It is missing the port number. I changed it by adding default port number as follows:



mongoose.createConnection('mongodb://localhost:27017/library_database');


and now I'm getting the response!


[#68645] Thursday, November 27, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ravenl

Total Points: 338
Total Questions: 107
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;