This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathapp.js
60 lines (56 loc) · 1.73 KB
/
app.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
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
var url = require('url');
var fs = require('fs');
var path = require('path');
var util = require("./lib/util");
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function (worker, code, signal) {
console.log('worker ' + worker.pid + ' died');
});
} else {
http.createServer(function (request, response) {
var pathname = url.parse(request.url).pathname;
if (pathname.charAt(pathname.length - 1) == "/")pathname += "index.html";
var filename = __dirname + pathname;
fs.exists(filename, function (exists) {
if (!exists) {
write404(response);
} else {
fs.stat(filename, function (err, stats) {
if (err) {
write404(response);
} else {
if (stats.isDirectory()) {
write404(response);
} else {
fs.readFile(filename, "binary", function (err, file) {
if (err) {
write404(response);
} else {
writeFile(200, filename, response, file);
}
});
}
}
});
}
});
}).listen(80);
}
function write404(response) {
fs.readFile(__dirname + "/404.html", "binary", function (err, file) {
writeFile(404, __dirname + "/404.html", response, file);
});
}
function writeFile(status, filename, response, file) {
var contentType = util.getContentType(filename);
response.writeHead(status, {'Content-Type' : contentType});
response.write(file, "binary");
response.end();
}