[英]What is the use of using process.env.PORT?
When we set the environment variable PORT using something like set PORT=5000
then we are telling the program directly which port to use.当我们使用set PORT=5000
之类的设置环境变量 PORT 时,我们直接告诉程序使用哪个端口。 So how is this any different from directly telling it to use port 3000?那么这与直接告诉它使用端口 3000 有什么不同呢?
Environment variables are used to simplify complexity and padronize values alongside one or many environments, for example, if your service runs on 2 environments, lets say qa and production, with environment variables you can simple write 2.env files and use diferent values for each one.环境变量用于简化复杂性并将值与一个或多个环境一起使用,例如,如果您的服务在 2 个环境上运行,比如说 qa 和生产环境,您可以使用环境变量简单地编写 2.env 文件并为每个环境使用不同的值一。
Answering your question, yes, if you set an environment variable for a port or set it in an in-code variable, in both ways you're telling the program what port to use.回答您的问题,是的,如果您为端口设置环境变量或将其设置在代码内变量中,那么这两种方式都是您告诉程序使用哪个端口。 Well, for your code, doens't matter from where the port are beeing set.好吧,对于您的代码,从哪里设置端口并不重要。
So... Why you would do that using env variables?所以......为什么你会使用环境变量来做到这一点?
Well, that's the main question I think, env variables provide a reliable way to:嗯,这是我认为的主要问题,环境变量提供了一种可靠的方法:
Actually they can do more than it.事实上,他们可以做的还不止这些。 You can check a nice reading about it here: https://medium.com/chingu/an-introduction-to-environment-variables-and-how-to-use-them-f602f66d15fa您可以在此处查看有关它的精彩阅读: https://medium.com/chingu/an-introduction-to-environment-variables-and-how-to-use-them-f602f66d15fa
A suggestion is, use env variables whenever you have to use one of these on your code:一个建议是,每当您必须在代码中使用其中一个变量时,请使用 env 变量:
But as you can see, it's not mandatory.但正如您所见,这不是强制性的。 Hope it can help you.希望它可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.