Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

javascript - How to set port for express server dynamically?

This is the question mostly asked by the beginners, as I had this question when I was starting out

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,

  • What is difference between using app.set('port', portNum) and directly using port number in app.listen(portNum)?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

Pro tip: Most hosting providers let you define custom environment variables too. You can test this locally by creating arbitrary variables like this:

$ 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]"

Option 2 - environment-specific config files (also highly recommended)

Using a config library like config and/or dotenv allows you to easily manage 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]"
}

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

Pro tip: Your configuration files can reference environment variables too! Continuing with our example from Option 1 above, you can define your production config like this:

production.json

{ 
    "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

Pro tip: - dotenv can be used alongside the config library for ultimate environment variable management!!


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'));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...