我正在学习如何将一个简单的后端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:...
如果使用heroku意味着我的后端处于生产模式,我的.env文件如何使用heroku config:set MONGODB_URI=mongodb+srv...
环境变量?第一句话指出环境变量仅用于开发模式。
我理解错了什么?开发模式和生产模式都使用环境变量,并且我阅读的注释的措词是错误的吗?
答案 0 :(得分:1)
我认为这是说.env
中的环境变量将在您开发时使用(而不是Heroku),并且如果您确实想在Heroku中使用环境变量,则需要通过{ {1}}。
答案 1 :(得分:0)
我理解错了什么?开发模式和生产模式都使用环境变量,并且我阅读的注释的措词是错误的吗?
在所有情况下都使用环境变量,但是仅在本地开发模式下,它们是从文件中读取的。 dotenv是一个软件包,允许从文件读取环境变量。
在生产模式下,Heroku本质上就是这样做的,例如:
PORT = 9999节点index.js
尝试一下,您可以使用以下命令在Node中访问此变量:
update dbo.sources set
category = replace(replace(category,'ABC01',''),', ','')
where category like '%ABC01%'
环境变量只是整个过程可用的全局变量。