-
Notifications
You must be signed in to change notification settings - Fork 89
/
server.js
75 lines (66 loc) · 2.25 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
mustache = require("./lib/mustache"),
keyprocessor = require("./lib/keyprocessor"),
sysinfo = require('./lib/sysinfo'),
port = process.env.PORT || 8888;
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(process.cwd(), uri);
path.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {
"Content-Type": "text/plain"
});
response.write("404 Not Found\n");
response.end();
return;
}
if (uri == "/") {
var t = path.join(filename, '/index.html');
var v = {
env: [],
sys: sysinfo.sysInfo()
};
for (var item in process.env) {
if(item.indexOf('STACKATO') == -1)
{
v['env'].push({
'key': item,
'value': keyprocessor.procKey(item, process.env[item])
});
}
}
fs.readFile(t, 'utf8', function(err, data) {
var html = mustache.to_html(data, v);
response.writeHead(200);
response.write(html, "binary");
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;
};
if (uri.match(/css$/)) {
response.writeHead(200, {
"Content-Type": "text/css"
});
} else {
response.writeHead(200);
}
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
console.log("Server running at\n => http://0.0.0.0:" + port + "/\nCTRL + C to shutdown");