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;
const adminEmail = process.env.ADMIN_EMAIL;
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);