Windows 2008 r2上的MongoDB连接错误:getaddrinfo ENOTFOUND

时间:2016-04-02 16:13:11

标签: node.js mongodb mongoose mean-stack meanjs

我正在撰写一份MEAN应用程序,我已经遇到了障碍。我的index.js文件,包含我的mongo数据库连接信息,似乎是错误的,我正在摸索着寻找解决方案。不幸的是,我看到的所有类似问题都在  nix方面。我不太熟悉Windows,所以如果这是一个愚蠢的问题,我道歉。

当前index.js文件:

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var _ = require('lodash');
var dbURI = 'mongodb:127.0.0.1\d$\db\pclistapp';
// Create the app
var app = express();

// Add middleware necessayr for the REST API
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(methodOverride('X-HTTP-Method_Override'));

// Connect to MongoDB
mongoose.connect(dbURI);
//var db = mongoose.connection;

// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function () {  
  console.log('Mongoose default connection open to ' + dbURI);
}); 

// If the connection throws an error
mongoose.connection.on('error',function (err) {  
  console.log('Mongoose default connection error: ' + err);
}); 

// When the connection is disconnected
mongoose.connection.on('disconnected', function () {  
  console.log('Mongoose default connection disconnected'); 
});

// If the Node process ends, close the Mongoose connection 
process.on('SIGINT', function() {  
  mongoose.connection.close(function () { 
    console.log('Mongoose default connection disconnected through app termination'); 
    process.exit(0); 
  }); 
});

// Test connection
mongoose.connection.once('open', function() {
    console.log('Listening on port 3000... ');
    app.listen(3000);
})

当我尝试运行节点index.js时的输出是MonogError:getaddrinfo ENOTFOUND mongodb mongodb:127。数据库当前正在端口27017上运行。

我试过了

mongodb:\\"PCname"\d$\db\pclistapp
mongodb:\\localhost\d$\db\pclistapp
mongodb:"PCname"\d$\db\pclistapp
mongodb:localhost\d$\db\pclistapp 

并且它们似乎都不起作用。我知道UNC适用于\" PCname" \ d $ \ db \ pclistapp所以我不确定是什么问题。

DB Connection Issue

DB Operational

1 个答案:

答案 0 :(得分:1)

花了很长时间,但错误很简单。我重新编写了代码以压缩错误,它不是我需要通过HTTP连接的UNC。解决这个问题,我参加了比赛。

傻傻的我。

代码:

var mongoURI = "mongodb://localhost:27017/pclistapp";
var MongoDB = mongoose.connect(mongoURI).connection;
MongoDB.on('error', function(err) { console.log(err.message); });
MongoDB.once('open', function() {
  console.log("mongodb connection open");
});