-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatic_server.js
63 lines (53 loc) · 1.84 KB
/
static_server.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
// **Static Server** is just a plain nodejs server capable of serving
// html, javascript, css, and plain text files with the correct mime
// definition.
//
// Default URL: http://localhost:8888/
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
fs.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) { filename += '/index.html'; }
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
// Content-type to be returned
// This is determined using a simple switch statement. This should
// really be abstracted to another module that can determine the
// content-type based on the basename(filename).
var content_type;
switch ( path.extname(filename) ) {
case ('.js'):
content_type = 'application/javascript';
break;
case ('.css'):
content_type = 'text/css';
break;
case ('.html'):
content_type = 'text/html';
break;
default:
content_type = 'text/plain';
}
response.writeHead(200, {"Content-Type": content_type});
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
console.log("Static file server running at\n=> http://localhost:" + port + "/\n=> CTRL + C to shutdown");