express:node throwing error on mongodb

   1: C:\Node_app\microblog\node_modules\connect-mongo\lib\connect-mongo.js:126
   2:         throw new Error('Error connecting to database');
   3:               ^
   4: Error: Error connecting to database
   5:     at module.exports.MongoStore.__proto__ (C:\Node_app\microblog\node_module
   6:     at Db.open (C:\Node_app\microblog\node_modules\connect-mongo\node_modules
   7:     at Server.connect.connectionPool.on.server._serverState (C:\Node_app\micr
   8: ion\server.js:408:7)
   9:     at EventEmitter.emit (events.js:115:20)
  10:     at connection.on.connectionStatus (C:\Node_app\microblog\node_modules\con
  11: :93:15)
  12:     at EventEmitter.emit (events.js:91:17)
  13:     at Socket.errorHandler (C:\Node_app\microblog\node_modules\connect-mongo\
  14:     at Socket.EventEmitter.emit (events.js:88:17)
  15:     at Socket._destroy.self.errorEmitted (net.js:329:14)
  16:     at process.startup.processNextTick.process._tickCallback (node.js:244:9)

与此类似node throwing error on mongodb,一直报Db.open那里出错,查源代码,发现应该是有err参数传入,因为之前从来没接触过mongodb--在某些文章中看到稍微有些了解,觉得是不是没有安装mongodb的问题呢?试了一下,果然如此。

windows下安装mongodb:

1、下载MongoDB,下载页面>>

2、解压安装,因为我电脑的C盘非系统盘,默认是我工作目录,所以我先解压到这个盘里。c:\mongodb (解压出来有一个bin目录,里面很多exe文件),然后再建立一个目录和文件

c:\mongodb\logs\mongodb.log

3、建立MongoDB的数据存放目录,假设为c:\mongodb_data\db

4、启动Mongo Db服务,如果控制台上看到类似的信息,则说明启动成功

 

默认MongoDB监听的端口是27017,mysql的是3306

5、测试连接

新开一个cmd窗口,进入mongodb的bin目录,输入mongo.exe,出现如下信息说明测试通过

继续测试

   1: >use test
   2: sitched to db test
   3: >db.foo.save({hello:1, word:2})
   4: >db.foo.find()
   5: "_id" : ObjectId("500a5fde4ad5d4c1884a5c3f"), "hello" : 1, "word" : 2 }

然后输入exit,退出窗口,然后再输入exit关闭该dos

6、注册MongoDB为系统服务。

   1: C:\mongodb\bin>mongod.exe --dbpath=c:\mongodb_data\db --logpath=c:\mongodb\logs\mongodb.log --install

如果需要卸载该服务,使用“sc delete MongoDB”即可

7、启动mongodb服务

net start MongoDB

打开任务管理器,可以看到进程已经启动

如果需要停止服务: net stop MongoDb

 

再次启动app,发现可以正常启动了

image

 

到这里express中使用mongodb来保存session就可以正常工作了,app.js的代码如下:

   1:  
   2: /**
   3:  * Module dependencies.
   4:  */
   5:  
   6: var express = require('express');
   7: var http = require('http');
   8: var routes = require('./routes');
   9:  
  10: var settings = require('./settings');
  11:  
  12: var MongoStore = require('connect-mongo')(express);
  13: //var connect = require('connect');
  14: //var MongoStore = require('connect-mongo')(connect);
  15:  
  16:  
  17: var sessionStore = new MongoStore({
  18:     db : settings.db
  19: }, function() {
  20:         console.log('connect mongodb success...');
  21:     });
  22:  
  23:  
  24:  
  25: var app = express();
  26:  
  27: app.configure(function(){
  28:   app.set('port', process.env.PORT || 3000);
  29:   app.set('views', __dirname + '/views');
  30:   app.set('view engine', 'jade');
  31:   app.use(express.favicon());
  32:   app.use(express.logger('dev'));
  33:   app.use(express.bodyParser());
  34:   app.use(express.methodOverride());
  35:  
  36:   app.use(express.cookieParser());
  37:   
  38:   app.use(express.session({
  39:     secret : settings.cookie_secret,
  40:     store : sessionStore,
  41:     cookie : {
  42:         maxAge : new Date(Date.now() + 1000 * 60 * 60)
  43:     }    
  44:   }));
  45:  
  46:   app.use(app.router);
  47:   app.use(express.static(__dirname + '/public'));
  48: });
  49:  
  50: app.configure('development', function(){
  51:   app.use(express.errorHandler());
  52: });
  53:  
  54: app.get('/', routes.index);
  55:  
  56:  
  57: http.createServer(app).listen(app.get('port'), function(){
  58:   console.log("Express server listening on port " + app.get('port'));
  59: });
posted @ 2012-07-21 16:02  meteoric_cry  阅读(5752)  评论(2编辑  收藏  举报