我正在尝试创建一个微博web应用程序宽度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)
它吸引我很多时间,我找不到办法摆脱这种情况。最后我必须在这里发布。任何指导表示赞赏。 :)
答案 0 :(得分:1)
您是否安装并配置了mongoDB服务器,详见此处:
http://docs.mongodb.org/manual/installation/
听起来好像没有在localhost上运行MongoDb服务器。