@@ -24,62 +24,69 @@ try {
2424 process . exit ( 1 )
2525}
2626
27- // create a server with express
28- const app = express ( )
27+ const createServer = ( ) => {
28+ // create a server with express
29+ const app = express ( )
2930
30- // override the default express listen method to use our server
31- app . listen = function ( port = process . env . PORT ||
32- /* istanbul ignore next: cannot be tested on Travis */ 443 ) {
33- app . server = https . createServer ( certOptions , app ) . listen ( port )
34- console . info ( "Server running on port " + port + "." )
35- return app . server
36- }
31+ // override the default express listen method to use our server
32+ app . listen = function ( port = process . env . PORT ||
33+ /* istanbul ignore next: cannot be tested on Travis */ 443 ) {
34+ app . server = https . createServer ( certOptions , app ) . listen ( port )
35+ console . info ( "Server running on port " + port + "." )
36+ return app . server
37+ }
3738
38- // use gzip compression minify
39- if ( process . env . NODE_ENV === "production" ) {
40- app . use ( compression ( { threshold : 1 } ) )
41- app . use ( minify ( ) )
42- app . set ( "json spaces" , 0 )
43- }
39+ // use gzip compression minify
40+ if ( process . env . NODE_ENV === "production" ) {
41+ app . use ( compression ( { threshold : 1 } ) )
42+ app . use ( minify ( ) )
43+ app . set ( "json spaces" , 0 )
44+ }
4445
45- /* SETUP USEFUL FUNCTIONS */
46+ /* SETUP USEFUL FUNCTIONS */
4647
47- // redirect http to https, usage `app.redirect()`
48- app . redirect = function (
49- /* istanbul ignore next: cannot be tested on Travis */ port = 80 ) {
50- app . http = http . createServer ( ( req , res ) => {
51- res . writeHead ( 301 , { Location : "https://" + req . headers [ "host" ] + req . url } )
52- res . end ( )
53- } ) . listen ( port )
54- console . info ( "http to https redirection active." )
55- }
48+ // redirect http to https, usage `app.redirect()`
49+ app . redirect = function (
50+ /* istanbul ignore next: cannot be tested on Travis */ port = 80 ) {
51+ app . http = http . createServer ( ( req , res ) => {
52+ res . writeHead ( 301 , {
53+ Location : "https://" + req . headers [ "host" ] + req . url
54+ } )
55+ res . end ( )
56+ } ) . listen ( port )
57+ console . info ( "http to https redirection active." )
58+ }
59+
60+ // serve static content, usage `app.serve([path])`
61+ app . serve = function ( staticPath = process . cwd ( ) , port = process . env . PORT ||
62+ /* istanbul ignore next: cannot be tested on Travis */ 443 ) {
63+ app . use ( express . static ( staticPath ) )
64+ // redirect 404 to 404.html or to index.html
65+ app . use ( ( req , res ) => {
66+ if ( ! staticPath . startsWith ( "/" ) )
67+ staticPath = process . cwd ( ) + "/" + staticPath
68+ const p404 = staticPath + "/404.html"
69+ const index = staticPath + "/index.html"
70+ // istanbul ignore else: not interesting
71+ if ( fs . existsSync ( p404 ) )
72+ res . status ( 404 ) . sendFile ( p404 )
73+ else if ( fs . existsSync ( index ) )
74+ res . status ( 404 ) . sendFile ( index )
75+ else res . status ( 404 ) . send ( req . path + " not found." )
76+ } )
77+ console . info ( "Serving static path: " + staticPath )
78+ app . listen ( port )
79+ }
5680
57- // serve static content, usage `app.serve([path])`
58- app . serve = function ( staticPath = process . cwd ( ) , port = process . env . PORT ||
59- /* istanbul ignore next: cannot be tested on Travis */ 443 ) {
60- app . use ( express . static ( staticPath ) )
61- // redirect 404 to 404.html or to index.html
62- app . use ( ( req , res ) => {
63- if ( ! staticPath . startsWith ( "/" ) )
64- staticPath = process . cwd ( ) + "/" + staticPath
65- const p404 = staticPath + "/404.html"
66- const index = staticPath + "/index.html"
67- // istanbul ignore else: not interesting
68- if ( fs . existsSync ( p404 ) )
69- res . status ( 404 ) . sendFile ( p404 )
70- else if ( fs . existsSync ( index ) )
71- res . status ( 404 ) . sendFile ( index )
72- else res . status ( 404 ) . send ( req . path + " not found." )
73- } )
74- console . info ( "Serving static path: " + staticPath )
75- app . listen ( port )
81+ return app
7682}
7783
7884/* MAIN */
7985
8086// usage: `serve [<path>]` or `node index.js [<path>]`
8187// istanbul ignore if: cannot be tested
8288if ( require . main === module ) {
89+ const app = createServer ( )
8390 // retrieve the static path from the process argv or use the cwd
8491 // 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path
8592 app . serve ( process . argv . length === 3 ? process . argv [ 2 ] : process . cwd ( ) )
@@ -88,4 +95,4 @@ if (require.main === module) {
8895}
8996
9097// export as module
91- module . exports = app
98+ module . exports = createServer
0 commit comments