简体   繁体   中英

NodeJS module.exports

I have 2 functions

    module.exports = (app, express) ->
  config = nconf.get("database:#{app.settings.env}")
  switch app.settings.env
    when 'production'
      app.db = mongoskin.db("#{config.host}:#{config.port}/#{config.name}",  {journal: false})
    else 
      app.db = mongoskin.db("#{config.host}:#{config.port}/#{config.name}",  {journal: false})

module.exports.db = (db , env) ->
  config = nconf.get("database:#{env}")
  switch env
    when 'production'
      db = mongoskin.db("#{config.host}:#{config.port}/#{config.name}",  {journal: false})
    else 
      db = mongoskin.db("#{config.host}:#{config.port}/#{config.name}?",  {journal: false})
  return db

At some places we are using the 2nd function. However, we would like to get rid of one function. How can we go about doing it without passing the app parameter ? Is there a better way to define the 1st function ?

Thanks, Raja.

添加了DBSingleton类,并在各处使用该对象。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM