NodeJS + ExpressJS – 无法读取环境variables

我正在快速设置一个variables,如下所示:

app.set('HOST', 'demo.sample.com'); 

但是,如果我尝试读取这个variables,我得到undefined的输出。 我正在使用process.env.HOST读取variables。 但是,如果我尝试使用app.get('HOST'), I get the correct value - how do I get the value using读取它app.get('HOST'), I get the correct value - how do I get the value using process.env.HOST` app.get('HOST'), I get the correct value - how do I get the value using

我不能使用`app.get('HOST'),因为我正在读取另一个文件中的variables – 一个不包含对快速应用程序variables的引用的文件。

问候,Mithun

app.set()不设置环境variables,它只是Express提供的便捷方法,与app.get()一起使用。

如果你想在你的JS代码中设置一个环境variables,使用这个:

 process.env.HOST = 'demo.sample.com'; 

更新:process.env只读取您的操作系统环境设置。 要设置express vars,请使用app.get()和app.set(),就像在其他答案中一样。

你的HOSTvariables是否设置? 它通常设置为HOSTNAME。

 [zlatko@droplet ~]$ node > process.env.HOST undefined > process.env.HOSTNAME 'droplet.zlayer.net' > 

你可以在Bash中手动设置(我假设你使用bash):

 [zlatko@droplet ~]$ export HOST=$HOSTNAME [zlatko@droplet ~]$ node > process.env.HOST 'droplet.zlayer.net' 

>

出口线可能是一个字面的,即:

 export HOSTNAME=myhostname.myserver.com 

如果你在Linux上,你将需要设置bash / shell的环境variables,以便从APP调用它。 尝试使用export NODE_ENV ='production'或'development'设置环境variables,然后使用app.configure'production',() – >configuration您的应用程序,然后重试