11#!/usr/bin/env node
22
3- const path = require ( 'path' )
4- const fs = require ( 'fs' )
5- const http = require ( 'http' )
6- const https = require ( 'spdy' ) // using HTTP/2: spdy will be deprecated soon, waiting for HTTP/2 on https module.
7- const express = require ( 'express' )
8- const compression = require ( 'compression' )
9-
10- // SSL CERTIFICATE
3+ const path = require ( "path" )
4+ const fs = require ( "fs" )
5+ const http = require ( "http" )
6+ // using HTTP/2: spdy will be deprecated soon,
7+ // waiting for HTTP/2 on https module.
8+ const https = require ( "spdy" )
9+ const express = require ( "express" )
10+ const compression = require ( "compression" )
11+
12+ // SSL certificate
1113const certOptions = {
12- key : fs . readFileSync ( path . resolve ( __dirname + "/ cert/server.key") ) ,
13- cert : fs . readFileSync ( path . resolve ( __dirname + "/ cert/server.crt") )
14+ key : fs . readFileSync ( path . resolve ( __dirname , " cert/server.key") ) ,
15+ cert : fs . readFileSync ( path . resolve ( __dirname , " cert/server.crt") )
1416}
1517
1618const port = process . env . PORT || 443
1719
18-
19- // START THE APP
20-
2120// run express
2221const app = express ( )
2322app . server = https . createServer ( certOptions , app ) . listen ( port )
2423
2524// save sockets for fast close
2625const sockets = [ ]
2726let nextSocketId = 0
28- app . server . on ( ' connection' , socket => {
27+ app . server . on ( " connection" , socket => {
2928 const socketId = nextSocketId ++
3029 sockets [ socketId ] = socket
31- socket . on ( ' close' , ( ) => delete sockets [ socketId ] )
30+ socket . on ( " close" , ( ) => delete sockets [ socketId ] )
3231} )
3332
3433// gzip compression and minify
3534app . use ( compression ( ) )
36- app . set ( ' json spaces' , 0 )
35+ app . set ( " json spaces" , 0 )
3736
3837// redirect http to https
3938if ( port === 443 || process . env . HTTP_PORT ) {
40- app . http = http . createServer ( function ( req , res ) {
41- res . writeHead ( 301 , { " Location" : "https://" + req . headers [ ' host' ] + req . url } )
39+ app . http = http . createServer ( ( req , res ) => {
40+ res . writeHead ( 301 , { Location : "https://" + req . headers [ " host" ] + req . url } )
4241 res . end ( )
4342 } ) . listen ( process . env . HTTP_PORT || 80 )
4443
45- app . http . on ( ' connection' , socket => {
44+ app . http . on ( " connection" , socket => {
4645 const socketId = nextSocketId ++
4746 sockets [ socketId ] = socket
48- socket . on ( ' close' , ( ) => delete sockets [ socketId ] )
47+ socket . on ( " close" , ( ) => delete sockets [ socketId ] )
4948 } )
5049}
5150
52- // SERVE STATIC FILES , if launched as: ' node index.js <static-path>'
53- if ( require . main === module ) { // called directly (not through require)
51+ // serve static files , if launched as: " node index.js <static-path>"
52+ if ( require . main === module ) {
5453 const staticPath = process . argv [ 2 ]
5554 app . use ( express . static ( staticPath || process . cwd ( ) ) )
5655}
5756
5857// ready
5958if ( ! process . env . TEST ) console . info ( "Server running on port " + port + "." )
6059
61-
62- // CLOSE THE APP
60+ // close the app
6361app . close = ( callback ) => {
6462 const promises = [
6563 new Promise ( resolve => app . http . close ( resolve ) ) ,
@@ -68,12 +66,11 @@ app.close = (callback) => {
6866 // destroy all opens
6967 for ( const socketId in sockets )
7068 sockets [ socketId ] . destroy ( )
71-
7269 return Promise . all ( promises ) . then ( ( ) => {
7370 if ( ! process . env . TEST ) console . info ( "Server closed." )
7471 if ( callback ) callback ( )
7572 } )
7673}
7774
7875// export as module
79- module . exports = app
76+ module . exports = app
0 commit comments