简体   繁体   English

NodeJS module.exports

[英]NodeJS module.exports

I have 2 functions 我有2个功能

    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 ? 我们如何在不传递app参数的情况下进行操作? Is there a better way to define the 1st function ? 有没有更好的方法来定义第一个函数?

Thanks, Raja. 谢谢,拉贾

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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