node环境变量_实际使用Node环境变量的方法如下

node环境变量

Environment variables are a fundamental part of Node development, but for some reason I never bothered with learning how to properly use them.

环境变量是Node开发的基本组成部分,但是由于某些原因,我从不费心去学习如何正确使用它们。

Maybe because they are called “Environment Variables.”

也许是因为它们被称为“环境变量”。

Just the words “Environment Variable” trigger a PTSD-laced flashback in which I am trying to add the correct path to the Java Home directory on Windows. Does it go in PATH or JAVA_HOME or both? Do I need to end it with a semicolon? WHY AM I USING JAVA?

只是单词“ Environment Variable”触发了PTSD标记的闪回,我试图在其中向Windows上的Java Home目录添加正确的路径。 它是放在PATH还是JAVA_HOME中? 我需要以分号结尾吗? 我为什么要使用JAVA?

In Node, environment variables can be global (like on Windows), but are often used with a specific process that you want to run. For instance, if you had a web application, you might have environment variables that define:

在Node中,环境变量可以是全局变量(例如在Windows上),但是通常与要运行的特定进程一起使用。 例如,如果您有一个Web应用程序,则可能具有定义以下内容的环境变量:

  • The HTTP Port to listen on

    侦听的HTTP端口
  • The Database Connection String

    数据库连接字符串
  • The JAVA_HOME…wait…no — sorry. The healing process takes time.

    JAVA_HOME…等等…不,对不起。 康复过程需要时间。

In this context, environment variables are really more like “Configuration Settings.” See how much nicer that sounds?

在这种情况下,环境变量实际上更像是“配置设置”。 看看听起来好多了?

If you’ve done .NET before, you might be familiar with something like a web.config file. Node environment variables work much the same was as settings in a web.config — they’re a way for you to pass in information that you don’t want to hard code.

如果您之前已经完成过.NET,则可能熟悉诸如web.config文件之类的东西。 节点环境变量的工作方式与web.config设置几乎相同,它们是您传递不需要硬编码的信息的一种方式。

But how do you use these variables in your Node application? I had a hard time finding good resources on this with the requisite amount of Java jokes, so I decided to create one. Here are some of the different ways you can define and then read environment variables in your Node applications.

但是,如何在Node应用程序中使用这些变量? 我很难找到必要的Java笑话,以此找到好的资源,因此我决定创建一个。 您可以使用以下几种不同的方法来定义和读取Node应用程序中的环境变量。

通过终端 (Pass it in the terminal)

You can pass environment variables on the terminal as part of your Node process. For instance, if you were running an Express app and wanted to pass in the port, you could do it like this…

您可以在终端上传递环境变量,作为您Node过程的一部分。 例如,如果您正在运行Express应用并想要通过端口,则可以这样操作…

PORT=65534 node bin/www

Fun fact: port 65535 is the largest TCP/IP network value available. How do I know that? StackOverflow of course. How does anybody know anything? But you can only go as high as port 65534 for a web app because that’s the highest port Chrome will connect to. How do I know that? Because Liran Tal told me in the comments. You should follow him. Between the two of us he’s the one who knows what he’s doing.

有趣的事实:端口65535是可用的最大TCP / IP网络值。 我怎么知道

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值