33const path = require ( "path" )
44const fs = require ( "fs" )
55const http = require ( "http" )
6- const https = require ( "spdy" ) // spdy allows http2, while waiting express to support the http2 module
6+ // spdy allows http2, while waiting express to support the http2 module
7+ const https = require ( "spdy" )
78const express = require ( "express" )
89const compression = require ( "compression" )
9- const minify = require ( 'express-minify' )
10-
10+ const minify = require ( "express-minify" )
1111
1212/* CONFIGURE THE SERVER */
1313
@@ -21,31 +21,30 @@ const certOptions = {
2121const app = express ( )
2222
2323// override the default express listen method to use our server
24- app . listen = function ( port = ( process . env . PORT || 443 ) ) {
25- app . server = https . createServer ( certOptions , app )
26- app . server . listen ( port )
24+ app . listen = function ( port = process . env . PORT || 443 ) {
25+ app . server = https . createServer ( certOptions , app ) . listen ( port )
2726 console . info ( "Server running on port " + port + "." )
27+ return app . server
2828}
2929
3030// use gzip compression minify
3131app . use ( compression ( ) )
3232app . use ( minify ( ) )
3333app . set ( "json spaces" , 0 )
3434
35-
3635/* SETUP USEFUL FUNCTIONS */
3736
3837// redirect http to https, usage `app.redirect()`
39- app . redirect = function ( ) {
38+ app . redirect = function ( ) {
4039 app . http = http . createServer ( ( req , res ) => {
41- res . writeHead ( 301 , { Location : "https://" + req . headers [ "host" ] + req . url } )
40+ res . writeHead ( 301 , { Location : "https://" + req . headers [ "host" ] + req . url } )
4241 res . end ( )
4342 } ) . listen ( 80 )
4443 console . info ( "http to https redirection active." )
4544}
4645
4746// serve static content, usage `app.serve([path])`
48- app . serve = function ( path = process . cwd ( ) , port ) {
47+ app . serve = function ( path = process . cwd ( ) , port ) {
4948 app . use ( express . static ( path ) )
5049 if ( port ) app . listen ( port )
5150 else app . listen ( )
@@ -54,9 +53,10 @@ app.serve = function (path=process.cwd(), port) {
5453
5554/* MAIN (running as script) */
5655// usage: `serve [<path>]` or `node index.js [<path>]`
56+ /* istanbul ignore if */
5757if ( require . main === module ) {
5858 // retrieve the static path from the process argv or use the cwd
59- // the first is node, the second is serve or index.js, the third (if exists) is the path
59+ // 1st is node, 2nd is serve or index.js, 3rd (if exists) is the path
6060 app . serve ( process . argv . length === 3 ? process . argv [ 2 ] : process . cwd ( ) )
6161 // redirect http to https
6262 app . redirect ( )
0 commit comments