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您的应用程序,然后重试
- 在Angular2中保护API密钥
- 当用户在命令提示符中select任何string时,节点Js不会为任何请求提供服务
- variables在脚本标记中不可用
- Express错误 – TypeError:express.Router不是一个函数
- Typescript Express中间件
- 使用angualr和expressionJS下载所有文件格式
- 如何在express3中启用csrf?
- 编写一个基于反应的聊天应用程序,实时更新Express,Mongodb,Nodejs
- Node.js(使用socket.io和express)抛出“events.js:71 throw arguments ; //未处理“错误”事件“