开发和生产模式之间环境变量用法的区别?

问题描述 投票:0回答:2

我正在学习如何将一个简单的后端node.js项目部署到生产环境(在我的情况下是heroku。我在.env文件中遇到了有关环境变量的注释:

The environment variables defined in dotenv will only be used when the backend is not in production mode, i.e. Heroku.

We defined the environment variables for development in file .env, but the environment variable that defines the database URL in production should be set to Heroku with the heroku config:set command:

heroku config:set MONGODB_URI=mongodb+srv:...

我的.env文件如何使用heroku config:set MONGODB_URI=mongodb+srv...环境变量,如果使用heroku意味着我的后端处于生产模式?第一句话指出环境变量仅用于开发模式。

我理解错了什么?开发模式和生产模式都使用环境变量,并且我阅读的注释的措词是错误的吗?]

node.js heroku environment-variables development-environment production-environment
2个回答
0
投票

[我认为这是指在开发中(而不是Heroku)将使用.env中的环境变量,并且,如果您确实想在Heroku中使用环境变量,则需要通过heroku config:set ...进行操作。


0
投票

我理解错了什么?开发模式和生产模式都使用环境变量,并且我阅读的注释的措词是错误的吗?]

环境变量在所有情况下都使用,但是仅在本地开发模式下,它们是从文件中读取的。dotenv是允许从文件中读取环境变量的软件包。

在生产模式下,Heroku本质上就是在做这件事,例如:

PORT = 9999节点index.js

尝试一下,您可以使用以下方法在Node中访问此变量:

console.log(process.env.PORT);
// 9999

环境变量只是整个过程可用的全局变量。

© www.soinside.com 2019 - 2024. All rights reserved.