1

我正在尝试运行 Addy Osmani 的书“Developing Backbone.js Applications”中的一些示例代码。一个练习提供了一个我曾经做过的 package.json 文件,npm install它安装了 Express、Mongoose 和 Path。从那里我node server.js从同一个目录运行,它给了我这个错误:

Express server listening on port 4711 in development mode

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: failed to connect to [localhost:27017]
    at null.<anonymous> (C:\backbone-fundamentals\exercise-2\node_modules\mongoose\node_modules\mongodb\lib\mongodb\connection\server.js:648:74)
    ...
    at process._tickCallback (node.js:415:13)

我假设它在连接到 Mongoose DB 时遇到问题,但作为 Node.js、Express 或 Mongoose 的业余爱好者,我不知道从哪里开始调试它。如何检查 Mongoose 是否安装正确?顺便说一句,我在 Windows 上。


如果您有兴趣,这里是 Node 的整个 server.js 文件:

'use strict';

// 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();

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

//Schemas
var Keywords = new mongoose.Schema({
    keyword: String
});

var Book = new mongoose.Schema({
    title: String,
    author: String,
    releaseDate: Date,
    keywords: [ Keywords ]
});

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

// 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 }));
});

// Routes
app.get( '/api', function( request, response ) {
    response.send( 'Library API is running' );
});

//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 );
        }
    });
});

//Get a single book by id
app.get( '/api/books/:id', function( request, response ) {
    return BookModel.findById( request.params.id, function( err, book ) {
        if( !err ) {
            return response.send( book );
        } else {
            return console.log( err );
        }
    });
});

//Insert a new book
app.post( '/api/books', function( request, response ) {
    var book = new BookModel({
        title: request.body.title,
        author: request.body.author,
        releaseDate: request.body.releaseDate,
        keywords: request.body.keywords
    });
    book.save( function( err ) {
        if( !err ) {
            return console.log( 'created' );
        } else {
            return console.log( err );
        }
    });
    return response.send( book );
});

//Update a book
app.put( '/api/books/:id', function( request, response ) {
    console.log( 'Updating book ' + request.body.title );
    return BookModel.findById( request.params.id, function( err, book ) {
        book.title = request.body.title;
        book.author = request.body.author;
        book.releaseDate = request.body.releaseDate;
        book.keywords = request.body.keywords;

        return book.save( function( err ) {
            if( !err ) {
                console.log( 'book updated' );
            } else {
                console.log( err );
            }
            return response.send( book );
        });
    });
});

//Delete a book
app.delete( '/api/books/:id', function( request, response ) {
    console.log( 'Deleting book with id: ' + request.params.id );
    return BookModel.findById( request.params.id, function( err, book ) {
        return book.remove( function( err ) {
            if( !err ) {
                console.log( 'Book removed' );
                return response.send( '' );
            } else {
                console.log( err );
            }
        });
    });
});

//Start server
var port = 4711;
app.listen( port, function() {
    console.log( 'Express server listening on port %d in %s mode', port, app.settings.env );
});
4

2 回答 2

5

您没有安装 MongoDB。

看看这个http://www.mongodb.org/downloads

于 2013-08-08T16:54:47.833 回答
1

在上述书的第 106 页,开发 Backbone.js 应用程序,它说要安装 MongoDB:

从 mongodb.org 下载并安装 MongoDB。网站上有详细的安装指南。

所以,一旦你这样做了,你在使用 Mongoose 时连接数据库应该不会有任何问题。

于 2013-08-08T17:19:33.563 回答