fixed 0000001
[JIRC.git] / public / server.js
1 var http = require("http"),
2 url = require("url"),
3 path = require("path"),
4 fs = require("fs"),
5 mime = require("mime")
6 port = process.argv[2] || 8888;
7
8 http.createServer(function(request, response) {
9
10 var uri = url.parse(request.url).pathname
11 , filename = path.join(process.cwd(), uri);
12
13 path.exists(filename, function(exists) {
14 if(!exists) {
15 response.writeHead(404, {"Content-Type": "text/plain"});
16 response.write("404 Not Found\n");
17 response.end();
18 return;
19 }
20
21 if (fs.statSync(filename).isDirectory()) filename += '/index.html';
22
23 fs.readFile(filename, "binary", function(err, file) {
24 if(err) {
25 response.writeHead(500, {"Content-Type": "text/plain"});
26 response.write(err + "\n");
27 response.end();
28 return;
29 }
30
31 response.writeHead(200, {"Content-Type": mime.lookup(filename)});
32 response.write(file, "binary");
33 response.end();
34 });
35 });
36 }).listen(parseInt(port, 10));