node.js 环境变量_在Node.js中获取和设置环境变量

node.js 环境变量

One of the best ways to use sensitive information in open source repositories without hard-coding the information within publicly available repositories is setting environment variables.  Set the environment variables on the server, retrieve them by key within your application.

在开放源代码存储库中使用敏感信息而不在公共可用存储库中对信息进行硬编码的最佳方法之一是设置环境变量。 在服务器上设置环境变量,然后在应用程序中通过键检索它们。

When using Node.js, you can retrieve environment variables by key from the process.env object:

使用Node.js时,可以通过键从process.env对象检索环境变量:


var mode = process.env.mode; // 'PRODUCTION', for example

var apiKey = process.env.apiKey; // '38294729347392432'


There are time when you may want to set environment variables while you run your node app -- these are set temporarily while the process is still running.  A common case is simulating environment variables during testing.  You can temporarily set these variables by pegging items onto the process.env object:

有时您可能需要在运行节点应用程序时设置环境变量-这些变量是在进程仍在运行时临时设置的。 常见的情况是在测试过程中模拟环境变量。 您可以通过将项目钉在process.env对象上来临时设置这些变量:


process.env.mode = 'TESTING';

// Now app code knows not to do destructive transactions!


Simple enough but worth documenting for future use!

很简单,但值得记录以备将来使用!

翻译自: https://davidwalsh.name/node-environment-variables

node.js 环境变量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值