0

我正在尝试制作一个微博网络应用宽度nodejs,express和mongodb。但是我遇到了一个问题,我的应用程序无法访问数据库。

我的 package.json 是这样的:

    {
    "name": "microblog"
  , "version": "0.0.1"
  , "private": true
  , "dependencies": {
      "express": "2.5.8"
    , "ejs": "0.0.1"
    , "connect-mongo" : "0.1.7"
    , "mongodb" : "0.9.9"
  }
}

配置如下:

    app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
            secret: settings.cookieSecret,
            store: new MongoStore({
                    db: settings.db
                })
          }));
  app.use(express.router(routes));
  app.use(express.static(__dirname + '/public'));

});

我在目录模型中使用 db.js 创建了一个 mongodb:

module.exports = {
    cookieSecret: 'microblog',
    db: 'microblog',
    host: 'localhost'
}

综上所述,我只是尝试调试我的应用程序以查看该应用程序是否可以成功连接到数据库。那么问题来了:

/home/edward/github/nodejs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:114
      throw new Error('Error connecting to database');
            ^
Error: Error connecting to database
    at /home/edward/github/nodejs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:114:13
    at /home/edward/github/nodejs/microblog/node_modules/mongodb/lib/mongodb/db.js:240:16
    at null.<anonymous> (/home/edward/github/nodejs/microblog/node_modules/mongodb/lib/mongodb/connection/server.js:390:7)
    at EventEmitter.emit (events.js:95:17)
    at null.<anonymous> (/home/edward/github/nodejs/microblog/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:96:15)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> (/home/edward/github/nodejs/microblog/node_modules/mongodb/lib/mongodb/connection/connection.js:388:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14
    at process._tickCallback (node.js:415:13)

这让我很费时间,我就是找不到摆脱这种情况的方法。最后我不得不在这里发布。任何指导表示赞赏。:)

4

1 回答 1

1

您是否按照此处的详细信息安装和配置了 mongoDB 服务器:

http://docs.mongodb.org/manual/installation/

听起来本地主机上没有运行 MongoDb 服务器。

于 2013-07-05T14:09:27.060 回答