-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple-http.js
executable file
·60 lines (51 loc) · 1.39 KB
/
simple-http.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
#!/usr/bin/env node
;(function() {
var fsio = require('fs');
var path = require('path');
var static = require('node-static');
function showUsageAndExit() {
console.log("usage: simple-http [-p <port-number>] [path]\n");
console.log(" -p <port-number>\tThe port number to use");
console.log(" [path]\t\tPath to serve. Defaults to current directory\n");
process.exit();
}
var dir = "./";
var port = 8080;
var args = process.argv.splice(2);
if (args.length > 0 && args.length < 4) {
for (var i = 0; i < args.length; i++) {
try {
switch (args[i]) {
case "-p":
port = parseInt(args[++i]);
break;
case "--help":
showUsageAndExit();
break;
default:
dir = args[i];
break;
}
} catch(e) {
showUsageAndExit();
}
}
}
dir = path.resolve(dir);
var stats = fsio.lstatSync(dir);
if (!stats.isDirectory()) {
showUsageAndExit();
}
//
// Create a node-static server instance to serve the './public' folder
//
var srv = new(static.Server)(dir);
require('http').createServer(function(request, response) {
request.addListener("end",
function() {
srv.serve(request, response);
// Serve files!
});
}).listen(port);
console.log("PID " + process.pid + " started on port " + port.toString() + " in '" + dir + "'");
})();