快递app听mongodb启动并重新建立连接

时间:2013-05-21 12:06:26

标签: node.js express

我有这个代码打开了与mongodb的连接:

db_connect_mongo = init: (callback) ->
  self = this
  mongo_options = db:
      safe: true
  mongoose.connect DB_URL, mongo_options
  db = self.db_mongo = mongoose.connection

  db.on "error", (error) ->
    logger.error "ERROR connecting to: " + DB_URL, logCategory
    callback error, null

  db.on "connected", ->
    logger.info "SUCCESSFULLY connected to: " + DB_URL, logCategory
    callback true, db

  db.on "disconnected", ->
    logger.info "DISCONNECTED from the database: " + DB_URL, logCategory

# check and connect to Redis

exports = module.exports = db_connect_mongo

然后我在app.coffee中调用它,如:

#Connect to database
dbconnection = require "./utils/dbconnect"
dbconnection.init (result) ->
  logger.info "Database initialized: " + result, logCategory

当我关闭mongo db时,我得到一个info: [DATABASE Connection] DISCONNECTED from the database: mongodb://127.0.0.1:27017/zmgc-mongo所以在我的logger.coffee中我想“听”如果mongodb出现以便建立连接。

什么是捕获所有de / re连接到db的事件的好方法?

0 个答案:

没有答案