1
1
#!/usr/bin/env node
2
2
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
11
13
const 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") )
14
16
}
15
17
16
18
const port = process . env . PORT || 443
17
19
18
-
19
- // START THE APP
20
-
21
20
// run express
22
21
const app = express ( )
23
22
app . server = https . createServer ( certOptions , app ) . listen ( port )
24
23
25
24
// save sockets for fast close
26
25
const sockets = [ ]
27
26
let nextSocketId = 0
28
- app . server . on ( ' connection' , socket => {
27
+ app . server . on ( " connection" , socket => {
29
28
const socketId = nextSocketId ++
30
29
sockets [ socketId ] = socket
31
- socket . on ( ' close' , ( ) => delete sockets [ socketId ] )
30
+ socket . on ( " close" , ( ) => delete sockets [ socketId ] )
32
31
} )
33
32
34
33
// gzip compression and minify
35
34
app . use ( compression ( ) )
36
- app . set ( ' json spaces' , 0 )
35
+ app . set ( " json spaces" , 0 )
37
36
38
37
// redirect http to https
39
38
if ( 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 } )
42
41
res . end ( )
43
42
} ) . listen ( process . env . HTTP_PORT || 80 )
44
43
45
- app . http . on ( ' connection' , socket => {
44
+ app . http . on ( " connection" , socket => {
46
45
const socketId = nextSocketId ++
47
46
sockets [ socketId ] = socket
48
- socket . on ( ' close' , ( ) => delete sockets [ socketId ] )
47
+ socket . on ( " close" , ( ) => delete sockets [ socketId ] )
49
48
} )
50
49
}
51
50
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 ) {
54
53
const staticPath = process . argv [ 2 ]
55
54
app . use ( express . static ( staticPath || process . cwd ( ) ) )
56
55
}
57
56
58
57
// ready
59
58
if ( ! process . env . TEST ) console . info ( "Server running on port " + port + "." )
60
59
61
-
62
- // CLOSE THE APP
60
+ // close the app
63
61
app . close = ( callback ) => {
64
62
const promises = [
65
63
new Promise ( resolve => app . http . close ( resolve ) ) ,
@@ -68,12 +66,11 @@ app.close = (callback) => {
68
66
// destroy all opens
69
67
for ( const socketId in sockets )
70
68
sockets [ socketId ] . destroy ( )
71
-
72
69
return Promise . all ( promises ) . then ( ( ) => {
73
70
if ( ! process . env . TEST ) console . info ( "Server closed." )
74
71
if ( callback ) callback ( )
75
72
} )
76
73
}
77
74
78
75
// export as module
79
- module . exports = app
76
+ module . exports = app
0 commit comments