#! /usr/bin/env node
// Quick-and-dirty web server using Node.js
// Republic Of Techies (RoT) group project.
var sys = require('sys');
var http = require('http');
var fs = require('fs');
var path = require('path');
var port = 8080;

http.createServer(function(request, response) {
    var url = request.url;
    if (url === '/')
        url = '/index.html';

    if (url === '/time') {
        response.writeHeader(200, {
            "Context-Type": "text/plain",
            "Access-Control-Allow-Origin": "http://localhost"});
        response.write(new Date().toString());
        response.end();
    }
    
    fs.readFile(path.join('.', url), function (err, data) {
        if (err) {
            if (err.code === 'ENOENT') {
                fs.readFile('page404.html', function(err, data) {
                    if (err)
                        throw err;
                    response.writeHeader(404, {
                        "Content-Type": "text/html"});
                    response.write(data.toString()
                                   .replace(/:PATH:/g, url));
                    response.end();
                });
            } else throw err;
        } else {
            // :TODO: determine content type --
            //   by file extension?
            //   by contents?
            //   a combination of both?
            response.writeHeader(200, {"Content-Type": "text/html"});
            response.write(data);
            response.end();
        }
    });
}).listen(port);
console.log('Server listening: http://localhost:' + port + '/');

@@ -102,16 +102,53 @@
 roject.%0A
+(function() %7B%0A    'use strict';%0A%0A    
 var sys 
@@ -161,24 +161,28 @@
 ire('sys');%0A
+    
 var http = r
@@ -193,24 +193,28 @@
 re('http');%0A
+    
 var fs = req
@@ -221,24 +221,28 @@
 uire('fs');%0A
+    
 var path = r
@@ -257,16 +257,20 @@
 path');%0A
+    
 var port
@@ -279,16 +279,187 @@
  8080;%0A%0A
+    var respond = function(response, code, headers, data) %7B%0A        response.writeHeader(code, headers);%0A        response.write(data);%0A        response.end();%0A    %7D;%0A%0A    
 http.cre
@@ -494,24 +494,28 @@
 response) %7B%0A
+    
     var url 
@@ -529,24 +529,28 @@
 st.url;%0A    
+ 
+   
 if (url === 
@@ -554,16 +554,20 @@
 == '/')%0A
+    
         
@@ -588,24 +588,28 @@
 html';%0A%0A    
+  
+  
 if (url === 
@@ -627,37 +627,38 @@
         
+    
 respon
-se.writeHeader(
+d(response, 
 200, %7B%0A 
@@ -664,24 +664,28 @@
             
+   
+ 
 %22Context-Typ
@@ -702,16 +702,20 @@
 plain%22,%0A
+    
         
@@ -768,27 +768,28 @@
 alhost%22%7D
-);%0A
+,%0A  
         
 response
@@ -780,31 +780,26 @@
         
-response.write(
+          
 new Date
@@ -826,35 +826,15 @@
     
-response.end();%0A    
 %7D
+%0A
 %0A    
-%0A
     
@@ -889,32 +889,36 @@
 data) %7B%0A        
+  
+  
 if (err) %7B%0A     
@@ -908,24 +908,28 @@
  if (err) %7B%0A
+    
             
@@ -953,24 +953,28 @@
 'ENOENT') %7B%0A
+    
             
@@ -993,23 +993,72 @@
 ile(
-'page404.html',
+%0A                        'page404.html',%0A                       
  fun
@@ -1088,32 +1088,40 @@
                 
+        
 if (err)%0A       
@@ -1137,16 +1137,24 @@
         
+   
+     
 throw er
@@ -1176,37 +1176,42 @@
         
-response.writeHeader(
+        respond(response, 
 404, %7B%0A 
@@ -1229,24 +1229,32 @@
             
+      
+  
 %22Content-Typ
@@ -1261,35 +1261,49 @@
 e%22: %22text/html%22%7D
-);%0A
+,%0A               
                 
@@ -1302,39 +1302,25 @@
             
-response.write(
+ 
 data.toStrin
@@ -1323,16 +1323,17 @@
 tring()%0A
+ 
         
@@ -1398,36 +1398,8 @@
     
-            response.end();%0A
     
@@ -1410,24 +1410,28 @@
         %7D);%0A
+    
             
@@ -1452,24 +1452,28 @@
 rr;%0A        
+    
 %7D else %7B%0A   
@@ -1469,16 +1469,20 @@
  else %7B%0A
+    
         
@@ -1525,32 +1525,36 @@
  --%0A            
+  
+  
 //   by file ext
@@ -1565,32 +1565,36 @@
 on?%0A            
+    
 //   by contents
@@ -1595,16 +1595,20 @@
 ntents?%0A
+    
         
@@ -1655,93 +1655,87 @@
     
+    
 respon
-se.writeHeader(200, %7B%22Content-Type%22: %22text/html%22%7D);%0A            response.write(
+d(response, 200, %7B%0A                    %22Content-Type%22: %22text/html%22%7D, 
 data
@@ -1745,39 +1745,25 @@
             
-response.end();
+%7D
 %0A        %7D%0A 
@@ -1764,17 +1764,15 @@
    %7D
+);
 %0A    
-%7D);%0A
 %7D).l
@@ -1784,16 +1784,20 @@
 (port);%0A
+    
 console.
@@ -1849,12 +1849,18 @@
 ort + '/');%0A
+%7D)();%0A

use strict