|
@@ -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);
|
|
|
|
|
|
-app.listen(port, 'localhost');
|
|
|
+/**
|
|
|
+ * 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(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);
|