@@ -21,55 +21,23 @@ const port = process.env.PORT || 443
2121const app = express ( )
2222app . server = https . createServer ( certOptions , app ) . listen ( port )
2323
24- // save sockets for fast close
25- const sockets = [ ]
26- let nextSocketId = 0
27- app . server . on ( "connection" , socket => {
28- const socketId = nextSocketId ++
29- sockets [ socketId ] = socket
30- socket . on ( "close" , ( ) => delete sockets [ socketId ] )
31- } )
32-
3324// gzip compression and minify
3425app . use ( compression ( ) )
3526app . set ( "json spaces" , 0 )
3627
3728// redirect http to https
38- if ( port === 443 || process . env . HTTP_PORT ) {
29+ if ( port === 443 || process . env . HTTP_PORT )
3930 app . http = http . createServer ( ( req , res ) => {
4031 res . writeHead ( 301 , { Location : "https://" + req . headers [ "host" ] + req . url } )
4132 res . end ( )
4233 } ) . listen ( process . env . HTTP_PORT || 80 )
4334
44- app . http . on ( "connection" , socket => {
45- const socketId = nextSocketId ++
46- sockets [ socketId ] = socket
47- socket . on ( "close" , ( ) => delete sockets [ socketId ] )
48- } )
49- }
50-
5135// serve static files, if launched as: "node index.js <static-path>"
52- if ( require . main === module ) {
53- const staticPath = process . argv [ 2 ]
54- app . use ( express . static ( staticPath || process . cwd ( ) ) )
55- }
36+ if ( require . main === module )
37+ app . use ( express . static ( process . argv [ 2 ] || process . cwd ( ) ) )
5638
5739// ready
5840console . info ( "Server running on port " + port + "." )
5941
60- // close the app
61- app . close = ( ) => {
62- const promises = [
63- new Promise ( resolve => app . http . close ( resolve ) ) ,
64- new Promise ( resolve => app . server . close ( resolve ) )
65- ]
66- // destroy all opens
67- for ( const socketId in sockets )
68- sockets [ socketId ] . destroy ( )
69- return Promise . all ( promises ) . then ( ( ) => {
70- console . info ( "Server closed." )
71- } )
72- }
73-
7442// export as module
7543module . exports = app
0 commit comments