简体   繁体   English

在 .env NodeJs 中处理多个环境变量

[英]Handle multiple environments variables in .env NodeJs

Suppose I have a.env file like these:假设我有一个像这样的 .env 文件:

#dev variable
PORT=3000
#production
PORT=3030

And I get these variables using process.env, how can I manage to sometimes use the dev variable and other times the Production variable我使用 process.env 获取这些变量,我怎样才能设法有时使用 dev 变量,有时使用 Production 变量

You can create multiple .env files like.dev.env, .prod.env, and load them based on NODE_ENV.您可以创建多个.env文件,例如 .dev.env、.prod.env,并根据 NODE_ENV 加载它们。 using this使用这个

Storing configuration in environment variables is the way to go, and exactly what is recommended by the config in the 12-Factor App , so you're already starting with the right foot.将配置存储在环境变量中是 go 的方式,这正是12-Factor App 中的配置所推荐的,所以你已经从右脚开始了。

The values of these variables should not be stored with the code, except maybe the ones for your local development environment, which you can even assume as the default values:这些变量的值不应与代码一起存储,除了可能用于本地开发环境的变量,您甚至可以将其假定为默认值:

port = process.env.PORT || '3000';

For all other environments, the values should be stored in a safe place like Vault or AWS Secrets Manager, and then are only handled by your deployment pipeline.对于所有其他环境,这些值应存储在Vault或 AWS Secrets Manager 等安全位置,然后仅由您的部署管道处理。 Jenkins, for example, has a credentials plugin to handle that.例如,Jenkins 有一个凭据插件来处理它。

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

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