How to set port for express server dynamically?

This is a question mostly asked by beginners as I had this question when I started

How to set a port for express without requiring a hard code or even choose a port yourself? This is the question that I had when I started in node and expressed (I’m still a beginner, I have a lot to learn). The things that I wanted to know, other than those included,

  • What is the difference between using app.set('port', portNum)and directly using a port number in app.listen(portNum)?
+4
source share
4 answers

1. port setting dynamically


1: ()

-, Heroku. , , , . , - ​​, :

$ PORT=1234 npm start

... :

const port = process.env.PORT;
app.listen(port);

: - . , :

$ FOO=bar ADMIN_EMAIL=joe@example.com npm start

... :

const foo = process.env.FOO;                  //-> "bar"
const adminEmail = process.env.ADMIN_EMAIL;   //-> "joe@example.com"

2 - , ( )

, ​​ , , . ( ):

|- config
   |- default.json
   |- testing.json
   |- production.json
|- src
   |- app.js

:

default.json

{
    "port": "3030",
    "adminEmail": "foo@example.com"
}

. , , , production , , 80:

testing.json

{
    "adminEmail": "tests@example.com"
}

production.json

{
    "port": 80
}

. , . , , . , "node", ( , , JSON):

$ NODE_ENV=testing npm start
$ NODE_ENV=production npm start

: ! 1 , :

production.json

{ 
    "port": "PORT"
}

"" . , :

$ NODE_ENV=production PORT=47861 npm start

2. app.set('port', 1234) vs app.listen(1234)


. NodeJS , app.set. Express ( ) app.set - . , app.get. , app.set('port', 1234) , "" : var port = 1234; - .

, . http- NodeJS, :

const http = require('http');
const server = http.createServer((req, res) => { ... });
server.listen(port);

( ) NodeJS , app.listen:

const express = require('express');
const app = express();
app.use((req, res) => { ... });
app.listen(port); // calls server.listen() for you
+5

,

1. ?

, 0 app.listen(), , , , , , , . app.address().port - :

let http = require('http');

let express = require('express');
let app = express();

app.use('/', serveStatic('./public'));
app.get('/', (req, res) => {res.render('index')});
let server = http.createServer(app);
server.listen(0, () => {
    console.log(server.address().port)
})

2. app.set('port', 1234) app.listen(1234)?

/ ( , ) , app.set() app.locals ( ), , app.set() , , .
app.locals app.set()?

+3

( unix):

$PORT = 1234 node app.js :

$export PORT = 1234 $ node app.js Windows:

PORT = 1234 Windows PowerShell:

$env: PORT = 1234

+1
source

config.js

let config = {};

// PORTS
config.port = {};
config.port.alpha = 4050;   // Server 1
config.port.beta = 4051;   // Server 2
.....


module.exports = config;

app.js

const express = require("express"),
    config = require("./config");

....

app.set('port',config.port[process.env.SERVER_NAME]);
app.listen(app.get('port'),function(){
    console.log("Server running at "+app.get('port'));
});

Server start

SERVER_NAME=alpha node app.js
SERVER_NAME=beta node app.js
0
source

Source: https://habr.com/ru/post/1671737/


All Articles