Nodejs - 使用“mongodb” - 模块没有连接到服务器并且继续抛出错误

时间:2013-12-18 12:33:26

标签: node.js mongodb express

作为新的节点开发人员,我无法修复数据库连接问题。我正在为我的应用程序开发使用expressjs,我也使用“mongodb”模块连接我的“mongodb” - 但我总是得到这样的错误: - 会出现什么问题?

我的错误讯息:

bsd-pcs2029048-vijaya-2:myNodeApp mohamedarif$ node app.js
========================================================================================
=  Please ensure that you set the default write concern for the database by setting    =
=   one of the options                                                                 =
=                                                                                      =
=     w: (value of > -1 or the string 'majority'), where < 1 means                     =
=        no write acknowledgement                                                       =
=     journal: true/false, wait for flush to journal before acknowledgement             =
=     fsync: true/false, wait for flush to file system before acknowledgement           =
=                                                                                      =
=  For backward compatibility safe is still supported and                              =
=   allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]      =
=   the default value is false which means the driver receives does not                =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:false})                           =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of no acknowledgement will change in the very near future                =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

events.js:71
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: listen EADDRINUSE
    at errnoException (net.js:770:11)
    at Server._listen2 (net.js:910:14)
    at listen (net.js:932:10)
    at Server.listen (net.js:998:5)
    at Function.app.listen (/Users/mohamedarif/Sites/myNodeApp/node_modules/express/lib/application.js:533:24)
    at Object.<anonymous> (/Users/mohamedarif/Sites/myNodeApp/app.js:32:5)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

这是我的app.js文件:

var http    = require("http"),
    express = require("express"),
    stylus = require("stylus"),
    myEmployee = require("./employee").myEmployee,
    app = express();

app.set('view engine', 'jade');
app.set('views', './views');
app.set('port', process.env.PORT || 1337);

app.use(stylus.middleware({
    src : __dirname + '/styles',
    dest : __dirname + '/styles',
    debug : true,
    force:true
}));

var employeeProvider = new myEmployee('localhost', 27017); // where i am initiating!

app.use(express.static(__dirname + '/styles'));
app.use(express.static(__dirname + '/css'));

app.get('/', function (req, res, next){
    res.end("<h1>Sample Tittle</h1>");
    next();
});

app.get("/sample", function(req, res) {
    res.render('index');
})

app.listen(1337); 

这是我的“myEmployee.js”:

var 
    Db = require("mongodb").Db,
    Server = require("mongodb").Server,
    Connect = require("mongodb").Connect,
    BSON = require("mongodb").BSON,
    ObjectID = require("mongodb").ObjectID;

myEmployee = function (host, port) {
    this.db = new Db('myEmployee', new Server(host, port, {safe : false}, {auto_reconnect : true}, {}));
    this.db.open(function(){});
} 

exports.myEmployee = myEmployee;

有人帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

有两个无关的问题。

问题#1:不再推荐您连接MongoDB的方式。

使用{safe: false},写入数据库的数据可能会丢失。例如,如果服务器端出现错误,您就不会知道。相反,你可以这样做:

this.db = new Db('myEmployee',
    new Server(host, port, {auto_reconnect : true}), {w:1});

请注意{w:1}。这意味着在群集中至少有一个MongoDB服务器确认写入之前,写命令不会成功返回。如果出现问题,则会返回错误。

但是有一种更好的方法可以连接到MongoDB。见MongoClient or how to connect in a new and better way。使用它,你的连接看起来像:

// at the top of your file:
var MongoClient = require('mongodb').MongoClient
  , Server = require("mongodb").Server
  // any other stuff you need to require()
  ;

// later, in your constructor or init function:
this.mongoClient = new MongoClient(new Server(host, port));

请注意MongoClient始终确认对数据库的写入,因此上述问题就消失了。

问题#2: EADDRINUSE错误只表示您已在端口1337上侦听其他内容。最有可能的是,您的应用程序的另一个实例在另一个终端或控制台窗口中运行。在给定的端口号上,您一次只能运行一个实例。