1

这是我第一次使用猫鼬运行节点。我正在学习这本骨干书中的一些教程,并且我正在学习使用 express、mongoose 创建一个 restful api 的这一章,并且我正在按照我什至已经开始复制和粘贴的代码进行操作,但它仍然无法正常工作。这是代码:

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

我一直在寻找堆栈溢出和其他试图解决问题的网站,但我发现似乎没有任何东西可以让我连接到 mongodb。

第一个错误是:

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)

此更改似乎解决了该问题:

    mongoose.connect( 'mongodb://localhost/library_database', function(err) { if (err) console.log(err); } );

在那之后,快递工作但mongodb无法连接:

Express server listening on port 4711 in development mode
[Error: failed to connect to [localhost:27017]]

我试着改成这样:

mongoose = require( 'mongoose' ).Mongoose;

我还尝试在 cli 中运行mongod,并对 cli 中的选项进行了一些更改,但这似乎只是打开了帮助页面。我完全被困住了......任何帮助将不胜感激。提前致谢。

4

2 回答 2

15

您实际上需要成功地让 mongod 运行并监听连接。只需键入mongod没有选项,按 ENTER,然后运行。然后在一个单独的终端中启动您的快速应用程序。请注意,这mongod是 mongodb 服务器守护进程,mongo而是命令行客户端,您可以在其中运行交互式 REPL 并发出数据库命令。

于 2013-08-31T05:52:48.287 回答
3

您需要安装数据库本身。请参阅此处获取 mongodb 安装指南。

于 2014-06-18T07:40:48.713 回答