猫鼬连接方法在简单的节点服务器上失败.快递,猫鼬,路径 [英] Mongoose connect method fails on simple Node Server. Express, Mongoose, Path

查看:59
本文介绍了猫鼬连接方法在简单的节点服务器上失败.快递,猫鼬,路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用猫鼬运行节点.我正在关注这本骨干书中的一些教程,并且本章将介绍使用express,猫鼬创建一个宁静的api的过程,并且我正在完全遵循我什至要复制和粘贴的代码,但是它仍然无法正常工作.这是代码:

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-后端

// 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.

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.

第一个错误是:

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中的选项有所不同,但这似乎只是显示了帮助页面.我完全被困住了……任何帮助将不胜感激.预先感谢.

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.

推荐答案

您实际上需要成功使mongod运行并监听连接.只需键入不带任何选项的mongod,按Enter,然后运行它.然后在单独的终端中启动您的Express应用程序.请注意,mongod是mongodb服务器守护程序,而mongo是命令行客户端,您可以在其中运行交互式REPL并发出数据库命令.

You need to actually successfully get mongod running and listening for connections. Just type mongod with no options, hit ENTER, and let it run. Then in a separate terminal start your express app. Note that mongod is the mongodb server daemon whereas mongo is the command line client where you can run an interactive REPL and issue database commands.

这篇关于猫鼬连接方法在简单的节点服务器上失败.快递,猫鼬,路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆