This repository has been archived by the owner on May 17, 2023. It is now read-only.
forked from http-party/http-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server
executable file
·199 lines (179 loc) · 5.61 KB
/
http-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env node
'use strict';
var colors = require('colors/safe'),
os = require('os'),
httpServer = require('../lib/http-server'),
portfinder = require('portfinder'),
opener = require('opener'),
argv = require('optimist')
.boolean('cors')
.argv;
var ifaces = os.networkInterfaces();
if (argv.h || argv.help) {
console.log([
'usage: http-server [path] [options]',
'',
'options:',
' -p Port to use [8080]',
' -a Address to use [0.0.0.0]',
' -d Show directory listings [true]',
' -i Display autoIndex [true]',
' -g --gzip Serve gzip files when possible [false]',
' -e --ext Default file extension if none supplied [none]',
' -s --silent Suppress log messages from output',
' --cors[=headers] Enable CORS via the "Access-Control-Allow-Origin" header',
' Optionally provide CORS headers list separated by commas',
' -o [path] Open browser window after starting the server',
' -c Cache time (max-age) in seconds [3600], e.g. -c10 for 10 seconds.',
' To disable caching, use -c-1.',
' -U --utc Use UTC time format in log messages.',
'',
' -P --proxy Fallback proxy if the request cannot be resolved. e.g.: http://someurl.com',
' --proxyOptions Pass options to proxy using nested dotted objects. e.g.: --proxyOptions.secure false',
'',
' -S --ssl Enable https.',
' -C --cert Path to ssl cert file (default: cert.pem).',
' -K --key Path to ssl key file (default: key.pem).',
'',
' -r --robots Respond to /robots.txt [User-agent: *\\nDisallow: /]',
' --no-dotfiles Do not show dotfiles',
' -h --help Print this list and exit.'
].join('\n'));
process.exit();
}
var port = argv.p || parseInt(process.env.PORT, 10),
host = argv.a || '0.0.0.0',
ssl = !!argv.S || !!argv.ssl,
proxy = argv.P || argv.proxy,
proxyOptions = argv.proxyOptions,
utc = argv.U || argv.utc,
logger;
var proxyOptionsBooleanProps = [
'ws', 'xfwd', 'secure', 'toProxy', 'prependPath', 'ignorePath', 'changeOrigin',
'preserveHeaderKeyCase', 'followRedirects', 'selfHandleResponse'
];
if (proxyOptions) {
Object.keys(proxyOptions).forEach(function (key) {
if (proxyOptionsBooleanProps.indexOf(key) > -1) {
proxyOptions[key] = proxyOptions[key] === 'true';
}
});
}
if (!argv.s && !argv.silent) {
logger = {
info: console.log,
request: function (req, res, error) {
var date = utc ? new Date().toUTCString() : new Date();
if (error) {
logger.info(
'[%s] "%s %s" Error (%s): "%s"',
date, colors.red(req.method), colors.red(req.url),
colors.red(error.status.toString()), colors.red(error.message)
);
}
else {
logger.info(
'[%s] "%s %s" "%s"',
date, colors.cyan(req.method), colors.cyan(req.url),
req.headers['user-agent']
);
}
}
};
}
else if (colors) {
logger = {
info: function () {},
request: function () {}
};
}
if (!port) {
portfinder.basePort = 8080;
portfinder.getPort(function (err, port) {
if (err) { throw err; }
listen(port);
});
}
else {
listen(port);
}
function listen(port) {
var options = {
root: argv._[0],
cache: argv.c,
showDir: argv.d,
autoIndex: argv.i,
gzip: argv.g || argv.gzip,
robots: argv.r || argv.robots,
ext: argv.e || argv.ext,
logFn: logger.request,
proxy: proxy,
proxyOptions: proxyOptions,
showDotfiles: argv.dotfiles
};
if (argv.cors) {
options.cors = true;
if (typeof argv.cors === 'string') {
options.corsHeaders = argv.cors;
}
}
if (ssl) {
options.https = {
cert: argv.C || argv.cert || 'cert.pem',
key: argv.K || argv.key || 'key.pem'
};
}
var server = httpServer.createServer(options);
server.listen(port, host, function () {
var canonicalHost = host === '0.0.0.0' ? '127.0.0.1' : host,
protocol = ssl ? 'https://' : 'http://';
logger.info([colors.yellow('Starting up http-server, serving '),
colors.cyan(server.root),
ssl ? (colors.yellow(' through') + colors.cyan(' https')) : '',
colors.yellow('\nAvailable on:')
].join(''));
if (argv.a && host !== '0.0.0.0') {
logger.info((' ' + protocol + canonicalHost + ':' + colors.green(port.toString())));
}
else {
Object.keys(ifaces).forEach(function (dev) {
ifaces[dev].forEach(function (details) {
if (details.family === 'IPv4') {
logger.info((' ' + protocol + details.address + ':' + colors.green(port.toString())));
}
});
});
}
if (typeof proxy === 'string') {
if (proxyOptions) {
logger.info('Unhandled requests will be served from: ' + proxy + '. Options: ' + JSON.stringify(proxyOptions));
}
else {
logger.info('Unhandled requests will be served from: ' + proxy);
}
}
logger.info('Hit CTRL-C to stop the server');
if (argv.o) {
opener(
protocol + canonicalHost + ':' + port,
{ command: argv.o !== true ? argv.o : null }
);
}
});
}
if (process.platform === 'win32') {
require('readline').createInterface({
input: process.stdin,
output: process.stdout
}).on('SIGINT', function () {
process.emit('SIGINT');
});
}
process.on('SIGINT', function () {
logger.info(colors.red('http-server stopped.'));
process.exit();
});
process.on('SIGTERM', function () {
logger.info(colors.red('http-server stopped.'));
process.exit();
});