22
33'use strict' ;
44
5- var colors = require ( 'colors/safe' ) ,
6- os = require ( 'os' ) ,
7- httpServer = require ( '../lib/http-server' ) ,
8- portfinder = require ( 'portfinder' ) ,
9- opener = require ( 'opener' ) ,
10- fs = require ( 'fs' ) ,
11- argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) ) ,
12- qrcode = require ( 'qrcode-terminal' ) ;
5+ var colors = require ( 'colors/safe' ) ,
6+ os = require ( 'os' ) ,
7+ httpServer = require ( '../lib/http-server' ) ,
8+ portfinder = require ( 'portfinder' ) ,
9+ opener = require ( 'opener' ) ,
10+ fs = require ( 'fs' ) ;
11+ var argv = require ( 'minimist' ) ( process . argv . slice ( 2 ) , {
12+ alias : {
13+ tls : 'ssl'
14+ }
15+ } ) ;
1316var ifaces = os . networkInterfaces ( ) ;
1417var plainIp ;
1518
@@ -40,7 +43,8 @@ if (argv.h || argv.help) {
4043 ' -U --utc Use UTC time format in log messages.' ,
4144 ' --log-ip Enable logging of the client\'s IP address' ,
4245 '' ,
43- ' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com' ,
46+ ' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com' ,
47+ ' --proxy-options Pass options to proxy using nested dotted objects. e.g.: --proxy-options.secure false' ,
4448 '' ,
4549 ' --username Username for basic authentication [none]' ,
4650 ' Can also be specified with the env variable NODE_HTTP_SERVER_USERNAME' ,
@@ -62,12 +66,26 @@ if (argv.h || argv.help) {
6266}
6367
6468var port = argv . p || argv . port || parseInt ( process . env . PORT , 10 ) ,
65- host = argv . a || '0.0.0.0' ,
66- ssl = argv . S || argv . ssl ,
67- proxy = argv . P || argv . proxy ,
68- utc = argv . U || argv . utc ,
69- version = argv . v || argv . version ,
70- logger ;
69+ host = argv . a || '0.0.0.0' ,
70+ tls = argv . S || argv . tls ,
71+ proxy = argv . P || argv . proxy ,
72+ proxyOptions = argv [ 'proxy-options' ] ,
73+ utc = argv . U || argv . utc ,
74+ version = argv . v || argv . version ,
75+ logger ;
76+
77+ var proxyOptionsBooleanProps = [
78+ 'ws' , 'xfwd' , 'secure' , 'toProxy' , 'prependPath' , 'ignorePath' , 'changeOrigin' ,
79+ 'preserveHeaderKeyCase' , 'followRedirects' , 'selfHandleResponse'
80+ ] ;
81+
82+ if ( proxyOptions ) {
83+ Object . keys ( proxyOptions ) . forEach ( function ( key ) {
84+ if ( proxyOptionsBooleanProps . indexOf ( key ) > - 1 ) {
85+ proxyOptions [ key ] = proxyOptions [ key ] . toLowerCase ( ) === 'true' ;
86+ }
87+ } ) ;
88+ }
7189
7290if ( ! argv . s && ! argv . silent ) {
7391 logger = {
@@ -130,6 +148,7 @@ function listen(port) {
130148 ext : argv . e || argv . ext ,
131149 logFn : logger . request ,
132150 proxy : proxy ,
151+ proxyOptions : proxyOptions ,
133152 showDotfiles : argv . dotfiles ,
134153 mimetypes : argv . mimetypes ,
135154 username : argv . username || process . env . NODE_HTTP_SERVER_USERNAME ,
@@ -144,7 +163,7 @@ function listen(port) {
144163 }
145164 }
146165
147- if ( ssl ) {
166+ if ( tls ) {
148167 options . https = {
149168 cert : argv . C || argv . cert || 'cert.pem' ,
150169 key : argv . K || argv . key || 'key.pem'
@@ -167,24 +186,26 @@ function listen(port) {
167186
168187 var server = httpServer . createServer ( options ) ;
169188 server . listen ( port , host , function ( ) {
170- var protocol = ssl ? 'https://' : 'http://' ;
189+ var protocol = tls ? 'https://' : 'http://' ;
171190
172- logger . info ( [ colors . yellow ( 'Starting up http-server, serving ' ) ,
173- colors . cyan ( server . root ) ,
174- ssl ? ( colors . yellow ( ' through' ) + colors . cyan ( ' https' ) ) : ''
191+ logger . info ( [
192+ colors . yellow ( 'Starting up http-server, serving ' ) ,
193+ colors . cyan ( server . root ) ,
194+ tls ? ( colors . yellow ( ' through' ) + colors . cyan ( ' https' ) ) : ''
175195 ] . join ( '' ) ) ;
176196
177197 logger . info ( [ colors . yellow ( '\nhttp-server version: ' ) , colors . cyan ( require ( '../package.json' ) . version ) ] . join ( '' ) ) ;
178198
179- logger . info ( [ colors . yellow ( '\nhttp-server settings: ' ) ,
180- ( [ colors . yellow ( 'CORS: ' ) , argv . cors ? colors . cyan ( argv . cors ) : colors . red ( 'disabled' ) ] . join ( '' ) ) ,
181- ( [ colors . yellow ( 'Cache: ' ) , argv . c ? ( argv . c === '-1' ? colors . red ( 'disabled' ) : colors . cyan ( argv . c + ' seconds' ) ) : colors . cyan ( '3600 seconds' ) ] . join ( '' ) ) ,
182- ( [ colors . yellow ( 'Connection Timeout: ' ) , argv . t === '0' ? colors . red ( 'disabled' ) : ( argv . t ? colors . cyan ( argv . t + ' seconds' ) : colors . cyan ( '120 seconds' ) ) ] . join ( '' ) ) ,
183- ( [ colors . yellow ( 'Directory Listings: ' ) , argv . d ? colors . red ( 'not visible' ) : colors . cyan ( 'visible' ) ] . join ( '' ) ) ,
184- ( [ colors . yellow ( 'AutoIndex: ' ) , argv . i ? colors . red ( 'not visible' ) : colors . cyan ( 'visible' ) ] . join ( '' ) ) ,
185- ( [ colors . yellow ( 'Serve GZIP Files: ' ) , argv . g || argv . gzip ? colors . cyan ( 'true' ) : colors . red ( 'false' ) ] . join ( '' ) ) ,
186- ( [ colors . yellow ( 'Serve Brotli Files: ' ) , argv . b || argv . brotli ? colors . cyan ( 'true' ) : colors . red ( 'false' ) ] . join ( '' ) ) ,
187- ( [ colors . yellow ( 'Default File Extension: ' ) , argv . e ? colors . cyan ( argv . e ) : ( argv . ext ? colors . cyan ( argv . ext ) : colors . red ( 'none' ) ) ] . join ( '' ) )
199+ logger . info ( [
200+ colors . yellow ( '\nhttp-server settings: ' ) ,
201+ ( [ colors . yellow ( 'CORS: ' ) , argv . cors ? colors . cyan ( argv . cors ) : colors . red ( 'disabled' ) ] . join ( '' ) ) ,
202+ ( [ colors . yellow ( 'Cache: ' ) , argv . c ? ( argv . c === '-1' ? colors . red ( 'disabled' ) : colors . cyan ( argv . c + ' seconds' ) ) : colors . cyan ( '3600 seconds' ) ] . join ( '' ) ) ,
203+ ( [ colors . yellow ( 'Connection Timeout: ' ) , argv . t === '0' ? colors . red ( 'disabled' ) : ( argv . t ? colors . cyan ( argv . t + ' seconds' ) : colors . cyan ( '120 seconds' ) ) ] . join ( '' ) ) ,
204+ ( [ colors . yellow ( 'Directory Listings: ' ) , argv . d ? colors . red ( 'not visible' ) : colors . cyan ( 'visible' ) ] . join ( '' ) ) ,
205+ ( [ colors . yellow ( 'AutoIndex: ' ) , argv . i ? colors . red ( 'not visible' ) : colors . cyan ( 'visible' ) ] . join ( '' ) ) ,
206+ ( [ colors . yellow ( 'Serve GZIP Files: ' ) , argv . g || argv . gzip ? colors . cyan ( 'true' ) : colors . red ( 'false' ) ] . join ( '' ) ) ,
207+ ( [ colors . yellow ( 'Serve Brotli Files: ' ) , argv . b || argv . brotli ? colors . cyan ( 'true' ) : colors . red ( 'false' ) ] . join ( '' ) ) ,
208+ ( [ colors . yellow ( 'Default File Extension: ' ) , argv . e ? colors . cyan ( argv . e ) : ( argv . ext ? colors . cyan ( argv . ext ) : colors . red ( 'none' ) ) ] . join ( '' ) )
188209 ] . join ( '\n' ) ) ;
189210
190211 logger . info ( colors . yellow ( '\nAvailable on:' ) ) ;
@@ -203,7 +224,12 @@ function listen(port) {
203224 }
204225
205226 if ( typeof proxy === 'string' ) {
206- logger . info ( 'Unhandled requests will be served from: ' + proxy ) ;
227+ if ( proxyOptions ) {
228+ logger . info ( 'Unhandled requests will be served from: ' + proxy + '. Options: ' + JSON . stringify ( proxyOptions ) ) ;
229+ }
230+ else {
231+ logger . info ( 'Unhandled requests will be served from: ' + proxy ) ;
232+ }
207233 }
208234 if ( options . showQR ) {
209235 qrcode . generate ( plainIp , { small : true } , function ( qr ) {
0 commit comments