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 环境变量

你可能感兴趣的:(java,python,vue,web,js,ViewUI)