Why does everyone store the port as an expressed variable?

Most people, and even the scaffold created by the express command line tool, do the following:

app.set(process.env.PORT || 3000);
...
...
...
http.createServer(app).listen(app.get('port'), ...);

Why? It seems redundant to me when it works just fine and less code:

http.createServer(app).listen(process.env.PORT || 3000, ...);

I am sure there is a reason, I just can not understand what it is.

+4
source share
4 answers

app.set('port', process.env.PORT || 3000), . , . , , . , , - . , , , , ( ) , , , , . , " - ".

, , - app.get('port'), , . , , process.env . , -, , app, .

+5

, " , , ?"

, , . , ( 100 000%). .

, , - , :)

.

+1

node.js, "PORT = 80 node app.js" , 3000 , 3000, "PORT = 80 node app.js" , 80.

0

, production . , , , , , , 1 .

, . / , , , . , , , (NCONF, ).

, config.js, :

// config.js
var nconf = require('nconf');

nconf.env().argv();  // read from environment table, then command line

nconf.defaults({
    'http': {'port': 3000},
    'mode': 'devel'
});

module.exports = nconf;

app.js , :

// app.js
var nconf = require('./config.js');

//no line app.set('port'...)

http.createServer(app).listen(nconf.get('http:port'), 
  function(){console.log('Server listening on port ' + nconf.get('http:port')}
  );

, app.set app.get, , . , : - , .

, , . . , :

nconf.env().file({file: 'appconfig.json'}).argv();

.

0
source

All Articles