简体   繁体   English

ExpressJs:不同环境中的配置变量

[英]ExpressJs: Config variables in different environments

Can't I define a variable to be used in different envs when I define the env? 定义环境时,不能定义要在不同环境中使用的变量吗?

app.configure 'development', () ->
 app.use express.errorHandler({dumpExceptions: true, showStack: true})
 mongoose.connect 'mongodb://xxx:xxx-db2011@sun.memberd.mongohq.com:10012/xxxx'
 test = "ola23"

app.configure 'production', () ->
 app.use express.errorHandler()
 mongoose.connect 'mongodb://xxx:xxx-db2011@sun.memberd.mongohq.com:10012/xxxx'
 test = "ola"

I can define the " mongoose.connect ", why can't I define test ? 我可以定义“ mongoose.connect ”,为什么不能定义test

That code sets a local variable to that configuration function to a value. 该代码将该配置函数的局部变量设置为一个值。 I'm sure that works. 我确定可以。 But how do you need to use that test variable? 但是,您如何使用该test变量?

The way you've done it here, it simply will go away as soon as this function finishes, you aren't sending or saving it anywhere. 您在这里完成此操作的方式只是在此功能完成后就消失了,您无需在任何地方发送或保存它。 The mongoose.connect line, does something, it passes in a string to a function that uses that string to do something awesome. mongoose.connect行执行某些操作,它将字符串传递给使用该字符串执行出色功能的函数。 test = "ola" only sets a local variable. test = "ola"仅设置局部变量。

So without knowing how you want to use test it's hard to advise more. 因此,在不知道如何使用test的情况下,很难提出更多建议。 But you probably want this instead: 但是您可能想要这样:

app.set 'test', 'ola'

Which you can then retrive "ola" later with: 然后,您可以通过以下方式检索"ola"

app.get 'test'

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

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