【问题标题】:NodeJS export similar routes in on declarationNodeJS 在声明中导出类似的路由
【发布时间】:2013-06-06 13:22:50
【问题描述】:

我的 /routes/index.coffee 文件中有这段代码:

exports.Dropbox = (req, res) ->
  production = if process.env['NODE_ENV'] == "production" then true
  if production
    mixpanelId = PROD_MIXPANEL_ID
  res.render 'connectors/Dropbox', { title: 'About Dropbox', mixpanelId: mixpanelId, production: production }

exports.Box = (req, res) ->
  production = if process.env['NODE_ENV'] == "production" then true
  if production
    mixpanelId = PROD_MIXPANEL_ID
  res.render 'connectors/Box', { title: 'About Box', mixpanelId: mixpanelId, production: production }

它被许多不同的提供商多次复制。任何想法如何在某种函数或数组中复制它,所以我不需要多次声明它?

【问题讨论】:

    标签: node.js express coffeescript


    【解决方案1】:

    在您的应用程序设置中配置它。

    app.configure 'production', ->
      app.set 'mixpanelId', PROD_MIXPANEL_ID
    
    app.configure 'development', ->
      app.set 'mixpanelId', DEV_MIXPANEL_ID
    
    // and in your handlers:
    exports.Dropbox = (req, res) ->
      mixpanelId = req.app.get 'mixpanelId'
      ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多