-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmulti-file-server.js
36 lines (32 loc) · 1019 Bytes
/
multi-file-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
var util = require("util"),
http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
multi = require("multi-node");
var dir = process.argv[2] || './public';
var port = parseInt(process.argv[3]) || 8080;
util.log('Serving files from ' + dir + ', port is ' + port);
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), dir, uri);
path.exists(filename, function(exists) {
if(exists) {
fs.readFile(filename, function(err, data) {
if (err) {
util.log('Error serving file ' + filename + ' ' + err);
util.log('request: ' + uri);
}
response.writeHead(200, {
'X-Node-Id': process.pid
});
response.end(data);
});
} else {
response.writeHead(404);
response.end();
}
});
});
var nodes = multi.listen({ port: port, nodes: 10 }, server);
util.log("Server " + process.pid + " running at http://localhost:" + port);