forked from bahamas10/node-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver.js
executable file
·117 lines (104 loc) · 3.32 KB
/
httpserver.js
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
#!/usr/bin/env node
/**
* Simple HTTP Server
*
* Author: Dave Eddy <[email protected]>
* Date: 4/10/2013
* License: MIT
*/
var http = require('http');
var os = require('os');
var accesslog = require('access-log');
var getopt = require('posix-getopt');
var package = require('./package.json');
function usage() {
return [
'usage: httpserver [options] [port] [host]',
'',
'command line HTTP server tool for serving up local files, similar to python -mSimpleHTTPServer',
'',
'options',
' -d, --no-index disable reading of index.html/index.htm if found, env HTTPSERVER_NO_INDEX',
' -h, --help print this message and exit',
' -H, --host <host> the host address on which to listen, env HTTPSERVER_HOST defaults to ' + (opts.host || '0.0.0.0'),
' -n, --no-dir-listing return 403 for directory requests instead of a directory listing, env HTTPSERVER_NO_DIR_LISTING',
' -p, --port <port> the port on which to listen, env HTTPSERVER_PORT, defaults to ' + (opts.port || 8080),
' -u, --updates check for available updates on npm',
' -v, --version print the version number and exit'
].join('\n');
}
// command line arguments
var options = [
'd(disable-index)',
'h(help)',
'H:(host)',
'n(no-indexes)',
'p:(port)',
'u(updates)',
'v(version)'
].join('');
var parser = new getopt.BasicParser(options, process.argv);
var opts = {
disableindex: process.env.HTTPSERVER_NO_INDEX,
host: process.env.HTTPSERVER_HOST || process.env.NODE_HOST,
nodir: process.env.HTTPSERVER_NO_DIR_LISTING,
port: process.env.HTTPSERVER_PORT || process.env.NODE_PORT,
};
var option;
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'd': opts.disableindex = true; break;
case 'h': console.log(usage()); process.exit(0);
case 'H': opts.host = option.optarg; break;
case 'n': opts.nodir = true; break;
case 'p': opts.port = option.optarg; break;
case 'u': // check for updates
require('latest').checkupdate(package, function(ret, msg) {
console.log(msg);
process.exit(ret);
});
return;
case 'v': console.log(package.version); process.exit(0);
default: console.error(usage()); process.exit(1); break;
}
}
var args = process.argv.slice(parser.optind());
var staticroute = require('static-route')(
{
autoindex: !opts.nodir,
logger: function() {},
tryfiles: opts.disableindex ? [] : ['index.html', 'index.htm']
}
);
opts.host = args[1] || opts.host || '0.0.0.0';
opts.port = args[0] || opts.port || 8080;
// print all ipv4 addresses
var ipv4 = getipv4addresses();
Object.keys(ipv4).forEach(function(iface) {
console.log('%s: %s', iface, ipv4[iface]);
});
// start the server
http.createServer(onrequest).listen(opts.port, opts.host, listening);
function listening() {
console.log('server started: http://%s:%d', opts.host, opts.port);
}
function onrequest(req, res) {
accesslog(req, res);
staticroute(req, res);
}
// get all ipv4 addresses
function getipv4addresses() {
var i = os.networkInterfaces();
var ret = {};
Object.keys(i).forEach(function(name) {
var ip4 = null;
i[name].forEach(function(int) {
if (int.family === 'IPv4') {
ip4 = int.address;
return;
}
});
ret[name] = ip4;
});
return ret;
}