@@ -13,54 +13,56 @@ var ifaces = os.networkInterfaces();
13
13
14
14
if ( argv . h || argv . help ) {
15
15
console . log ( [
16
- " usage: http-server [path] [options]" ,
17
- "" ,
18
- " options:" ,
19
- " -p Port to use [8080]" ,
20
- " -a Address to use [0.0.0.0]" ,
21
- " -d Show directory listings [true]" ,
22
- " -i Display autoIndex [true]" ,
23
- " -e --ext Default file extension if none supplied [none]" ,
24
- " -s --silent Suppress log messages from output" ,
25
- " --cors Enable CORS via the ' Access-Control-Allow-Origin' header" ,
26
- " -o [path] Open browser window after starting the server" ,
27
- " -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds." ,
28
- " To disable caching, use -c-1." ,
29
- "" ,
30
- " -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com" ,
31
- "" ,
32
- " -S --ssl Enable https." ,
33
- " -C --cert Path to ssl cert file (default: cert.pem)." ,
34
- " -K --key Path to ssl key file (default: key.pem)." ,
35
- "" ,
36
- " -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]" ,
37
- " -h --help Print this list and exit."
16
+ ' usage: http-server [path] [options]' ,
17
+ '' ,
18
+ ' options:' ,
19
+ ' -p Port to use [8080]' ,
20
+ ' -a Address to use [0.0.0.0]' ,
21
+ ' -d Show directory listings [true]' ,
22
+ ' -i Display autoIndex [true]' ,
23
+ ' -e --ext Default file extension if none supplied [none]' ,
24
+ ' -s --silent Suppress log messages from output' ,
25
+ ' --cors Enable CORS via the " Access-Control-Allow-Origin" header' ,
26
+ ' -o [path] Open browser window after starting the server' ,
27
+ ' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.' ,
28
+ ' To disable caching, use -c-1.' ,
29
+ '' ,
30
+ ' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com' ,
31
+ '' ,
32
+ ' -S --ssl Enable https.' ,
33
+ ' -C --cert Path to ssl cert file (default: cert.pem).' ,
34
+ ' -K --key Path to ssl key file (default: key.pem).' ,
35
+ '' ,
36
+ ' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]' ,
37
+ ' -h --help Print this list and exit.'
38
38
] . join ( '\n' ) ) ;
39
39
process . exit ( ) ;
40
40
}
41
41
42
42
var port = argv . p || parseInt ( process . env . PORT , 10 ) ,
43
43
host = argv . a || '0.0.0.0' ,
44
- log = ( argv . s || argv . silent ) ? ( function ( ) { } ) : console . log ,
45
44
ssl = ! ! argv . S || ! ! argv . ssl ,
46
45
proxy = argv . P || argv . proxy ,
47
- requestLogger ;
46
+ logger = colors && { } ;
48
47
49
48
if ( ! argv . s && ! argv . silent ) {
50
- requestLogger = function ( req , res , error ) {
51
- var date = ( new Date ) . toUTCString ( ) ;
52
- if ( error ) {
53
- log ( '[%s] "%s %s" Error (%s): "%s"' , date , req . method . red , req . url . red , error . status . toString ( ) . red , error . message . red ) ;
54
- } else {
55
- log ( '[%s] "%s %s" "%s"' , date , req . method . cyan , req . url . cyan , req . headers [ 'user-agent' ] ) ;
49
+ logger = {
50
+ info : console . log ,
51
+ request : function ( req , res , error ) {
52
+ var date = new Date ( ) . toUTCString ( ) ;
53
+ if ( error ) {
54
+ logger . info ( '[%s] "%s %s" Error (%s): "%s"' , date , req . method . red , req . url . red , error . status . toString ( ) . red , error . message . red ) ;
55
+ } else {
56
+ logger . info ( '[%s] "%s %s" "%s"' , date , req . method . cyan , req . url . cyan , req . headers [ 'user-agent' ] ) ;
57
+ }
56
58
}
57
59
} ;
58
60
}
59
61
60
62
if ( ! port ) {
61
63
portfinder . basePort = 8080 ;
62
64
portfinder . getPort ( function ( err , port ) {
63
- if ( err ) throw err ;
65
+ if ( err ) { throw err ; }
64
66
listen ( port ) ;
65
67
} ) ;
66
68
} else {
@@ -75,7 +77,7 @@ function listen(port) {
75
77
autoIndex : argv . i ,
76
78
robots : argv . r || argv . robots ,
77
79
ext : argv . e || argv . ext ,
78
- logFn : requestLogger ,
80
+ logFn : logger . request ,
79
81
proxy : proxy
80
82
} ;
81
83
@@ -95,9 +97,9 @@ function listen(port) {
95
97
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host ,
96
98
protocol = ssl ? 'https:' : 'http:' ;
97
99
98
- log ( 'Starting up http-server, serving ' . yellow
100
+ logger . info ( 'Starting up http-server, serving ' . yellow
99
101
+ server . root . cyan
100
- + ( ssl ? ( ' through' . yellow + ' https' . cyan ) : '' )
102
+ + ssl ? ' through' . yellow + ' https' . cyan : ''
101
103
+ ' on: ' . yellow
102
104
+ ( protocol + '//' + host + ':' + port ) . cyan ) ;
103
105
@@ -112,10 +114,10 @@ function listen(port) {
112
114
} ) ;
113
115
114
116
if ( typeof proxy === 'string' ) {
115
- log ( 'Unhandled requests will be served from: ' + proxy ) ;
117
+ logger . info ( 'Unhandled requests will be served from: ' + proxy ) ;
116
118
}
117
119
118
- log ( 'Hit CTRL-C to stop the server' ) ;
120
+ logger . info ( 'Hit CTRL-C to stop the server' ) ;
119
121
if ( argv . o ) {
120
122
opener (
121
123
protocol + '//' + canonicalHost + ':' + port ,
@@ -134,8 +136,8 @@ if (process.platform === 'win32') {
134
136
} ) ;
135
137
}
136
138
137
- process . on ( 'SIGINT' , function ( ) {
138
- log ( 'http-server stopped.' . red ) ;
139
+ process . on ( 'SIGINT' , function ( ) {
140
+ logger . info ( 'http-server stopped.' . red ) ;
139
141
process . exit ( ) ;
140
142
} ) ;
141
143
0 commit comments