8
0
mirror of https://github.com/key-networks/ztncui.git synced 2024-08-31 04:28:00 +00:00

HTTPS_PORT and HTTPS_HOST in .env

This commit is contained in:
Key Networks
2017-12-29 21:39:16 +08:00
parent a8766b0a08
commit 49a3319e58
2 changed files with 50 additions and 21 deletions

52
bin/www
View File

@@ -16,22 +16,38 @@ const options = {
}
/**
* Get port from environment and store in Express.
* Get ports from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const sport = normalizePort(process.env.SPORT || '3443');
app.set('sport', sport);
const http_port = normalizePort(process.env.HTTP_PORT || '3000');
app.set('http_port', http_port);
const https_port = normalizePort(process.env.HTTPS_PORT || null);
app.set('https_port', https_port);
/**
* Create HTTPS server and listen on localhost only for HTTP and on all network interfaces for HTTPS
* Get interface address on which to listen for HTTPS requests from env.
*/
const https_host = process.env.HTTPS_HOST || null;
app.set('https_host', https_host);
/**
* Create HTTPS server and listen on localhost only for HTTP and
* on all network interfaces for HTTPS if HTTPS_PORT is set in env,
* or on specific interface if HTTPS_HOST is set in env.
*/
app.listen(port, 'localhost');
app.listen(http_port, 'localhost');
const server = https.createServer(options, app);
server.listen(sport);
if (https_port) {
if (https_host) {
console.log('Listening for HTTPS requests on port ' + https_port + ' on address ' + https_host);
} else {
console.log('Listening for HTTPS requests on port ' + https_port + ' on all interfaces');
}
server.listen(https_port, https_host);
}
server.on('error', onError);
server.on('listening', onListening);
@@ -40,7 +56,7 @@ server.on('listening', onListening);
*/
function normalizePort(val) {
let port = parseInt(val, 10);
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
@@ -56,7 +72,7 @@ function normalizePort(val) {
}
/**
* Event listener for HTTP server "error" event.
* Event listener for HTTP/S server "error" event.
*/
function onError(error) {
@@ -64,13 +80,13 @@ function onError(error) {
throw error;
}
let bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
const bind = typeof http_port === 'string'
? 'Pipe ' + http_port
: 'Port ' + http_port;
let sbind = typeof sport === 'string'
? 'Pipe ' + sport
: 'Port ' + sport;
const sbind = typeof https_port === 'string'
? 'Pipe ' + https_port
: 'Port ' + https_port;
// handle specific listen errors with friendly messages
switch (error.code) {
@@ -92,8 +108,8 @@ function onError(error) {
*/
function onListening() {
let addr = server.address();
let bind = typeof addr === 'string'
const addr = server.address();
const bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);