import * as http from 'http' ; import * as debugModule from 'debug' ; import app from './app' ; const debug = debugModule ( 'express-start:server' ) ; const port = normalizePort ( process . env . PORT || '3000' ) ; app . set ( 'port' , port ) ; const server = http . createServer ( app ) ; const sioModules = require ( 'require-all' ) ( { dirname : __dirname + '/socket.io' , filter : ( filename ) => { filename = filename . toLowerCase ( ) ; if ( ( filename . endsWith ( '.ts' ) && ! filename . endsWith ( '.spec.ts' ) ) || ( filename . endsWith ( '.js' ) && ! filename . endsWith ( '.spec.js' ) ) ) { return filename . substr ( 0 , filename . length - 3 ) ; } } } ) ; for ( const name of Object . keys ( sioModules ) ) { const exported = sioModules [ name ] . default ; if ( exported && exported . constructor . name === 'Server' ) { console . log ( `Add socket.io server ${ name }` ) ; const sioServer = exported as SocketIO . Server ; sioServer . attach ( server ) ; } } server . listen ( port ) ; server . on ( 'error' , onError ) ; server . on ( 'listening' , onListening ) ; /**
 * Normalize a port into a number, string, or false.
 */ function normalizePort ( val ) { const portInt = parseInt ( val , 10 ) ; if ( isNaN ( portInt ) ) { return val ; } if ( portInt >= 0 ) { return portInt ; } return false ; } /**
 * Event listener for HTTP server "error" event.
 */ function onError ( error ) { if ( error . syscall !== 'listen' ) { throw error ; } const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port ; switch ( error . code ) { case 'EACCES' : console . error ( bind + ' requires elevated privileges' ) ; process . exit ( 1 ) ; break ; case 'EADDRINUSE' : console . error ( bind + ' is already in use' ) ; process . exit ( 1 ) ; break ; default : throw error ; } } /**
 * Event listener for HTTP server "listening" event.
 */ function onListening ( ) { const addr = server . address ( ) ; const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr . port ; debug ( 'Listening on ' + bind ) ; console . log ( 'Listening on ' + bind ) ; }