问题描述
如何设置快递的端口,无需硬编码甚至自己选择端口?这是我在节点和表达开始时遇到的问题(我还是初学者,有很多东西需要学习)。除了包含的内容之外,我想知道的事情,
How to set port for express without needing to hardcode or even choose port yourself? This is the question I had when I was starting out in node and express (I am still a beginner, have lots of things to learn). Things I wanted know other than that included,
- 使用
应用程序 之间的区别是什么.set('port',portNum)
并直接使用中的端口号app.listen(portNum)
?
- What is difference between using
app.set('port', portNum)
and directly using port number inapp.listen(portNum)
?
推荐答案
如何为快递设置端口而无需硬编码甚至自己选择端口?
选项1:环境变量(推荐)
通常情况下,您将应用程序部署到托管像这样的提供商。根据主机的配置方式,系统将动态设置环境变量,您的应用程序需要从此变量获取端口。例如,托管服务提供商在运行您的应用时可能会运行这样的命令:
How to set port for express without needing to hardcode or even choose port yourself?
Option 1: Environment variable (recommended)
Often times you will deploy your app to a hosting provider like Heroku. Depending on how the host is configured, the system will dynamically set an environment variable and your app will need to get the port from this variable. For example, the hosting provider might run a command like this when it runs your app:
$ PORT=1234 npm start
...在您的代码中,您可以像这样访问此变量:
... and within your code, you can access this variable like this:
const port = process.env.PORT;
app.listen(port);
$ FOO=bar [email protected] npm start
...并从以下代码中访问这些变量:
...and access those variables from code like this:
const foo = process.env.FOO; //-> "bar"
const adminEmail = process.env.ADMIN_EMAIL; //-> "[email protected]"
选项2 - 特定于环境的配置文件(强烈推荐)
使用配置库,如允许您拥有特定于环境的配置选项。你的文件夹结构如下所示(注意文件的名称):
Option 2 - environment-specific config files (also highly recommended)
Using a config library like this one allows you to have environment-specific config options. Your folder structure would look like this (note the names of the files):
|- config
|- default.json
|- testing.json
|- production.json
|- src
|- app.js
然后定义默认变量和特定于环境的变量:
You then define your "default" variables and environment-specific variables:
default.json
{
"port": "3030",
"adminEmail": "[email protected]"
}
testing.json
{
"port": "5555"
}
production.json
{
"adminEmail": "[email protected]"
}
配置库将始终使用默认变量。当您进行测试时,它将使用默认的管理员电子邮件和其他端口。当您使用生产时,它将使用默认端口,但使用不同的管理员电子邮件。您定义节点环境的方式是这样的(请注意我们使用与JSON配置文件相同的名称):
The config library will always use the default variables. When you are on testing it will use the default admin email and a different port. When you are on production it will use the default port but a different admin email. The way you define your "node environment" is like this (notice we use the same name as the JSON config files):
$ NODE_ENV=testing npm start
$ NODE_ENV=production npm start
production.json
{
"port": "PORT"
}
配置库将查找名为PORT的任何环境变量,并将使用该值。总而言之,运行应用程序的最终命令可能如下所示:
The config library will look for any environment variables named "PORT" and will use that value. Putting it all together, your final command to run your app might look like this:
$ NODE_ENV=production PORT=47861 npm start
2。使用 app.set('port',portNum)
和直接使用 app.listen(portNum)$中的端口号有什么区别c $ c>?
Express允许您使用 app.set设置应用程序变量
- 但这只是定义变量的一种奇特方式。您可以稍后使用 app.get
获取这些变量的值。
2. What is the difference between using app.set('port', portNum)
and directly using port number in app.listen(portNum)
?
Express allows you to set application variables using app.set
- but this is just a fancy way for defining variables. You can later get the values for these variables using app.get
.
迟早,您需要告诉您的应用监听特定端口的流量。你可以这样做:
Sooner or later, you are going to need to tell your app to listen for traffic on a specific port. You could do something like this:
const app = express();
app.set('port', process.env.PORT);
app.use((req, res) => { ... });
app.listen(app.get('port'));
这篇关于如何动态设置Express服务器的端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!