3 * Copyright 2023 highstreet technologies and others
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Module dependencies.
22 const app = require('../app');
23 const debug = require('debug')('viewer:server');
24 const http = require('http');
27 * Normalize a port into a number, string, or false.
29 const normalizePort = (val) => {
30 const port = parseInt(val, 10);
46 * Event listener for HTTP server "error" event.
48 const onError = (error) => {
49 if (error.syscall !== 'listen') {
53 const bind = typeof port === 'string'
57 // handle specific listen errors with friendly messages
60 console.error(bind + ' requires elevated privileges');
64 console.error(bind + ' is already in use');
73 * Event listener for HTTP server "listening" event.
75 const onListening = () => {
76 const addr = server.address();
77 const bind = typeof addr === 'string'
79 : 'port ' + addr.port;
80 debug('Listening on ' + bind);
84 * Get port from environment and store in Express.
86 const port = normalizePort(process.env.PORT || '3000');
87 app.set('port', port);
92 const server = http.createServer(app);
95 * Listen on provided port, on all network interfaces.
98 server.on('error', onError);
99 server.on('listening', onListening);