﻿var http = require('http'),
    fs = require('fs');

http
    .createServer(function(request, response) {
        if (request.url === '/favicon.ico') {
            response.writeHead(404, { 'Content-Type': 'text/html' });
            response.end();
            return;
        }

        var filename;
        if (request.url === '/') filename = 'index.html';
        else filename = '.' + request.url;

        if (filename.indexOf('second') !== -1) {
            try {
                setTimeout(function() {
                    var filecontent = fs.readFileSync(filename);

                    response.writeHead(200, { 'Content-Type': 'text/html' });
                    response.write(filecontent);
                    response.end();
                }, 5000);
            } catch (e) {
            }
        } else {
            try {
                var filecontent = fs.readFileSync(filename);

                response.writeHead(200, { 'Content-Type': 'text/html' });
                response.write(filecontent);
                response.end();
            } catch (e) { }

            filename = '..' + request.url;

            try {
                var filecontent = fs.readFileSync(filename);

                response.writeHead(200, { 'Content-Type': 'text/html' });
                response.write(filecontent);
                response.end();
            } catch (e) { }
        }
    })
    .listen(2000, '127.0.0.1');

console.log('Server running at http://127.0.0.1:2000/');
@@ -51,31 +51,22 @@
 );%0A%0A
-http%0A    .createServer(
+var send404 = 
 func
@@ -74,17 +74,8 @@
 ion(
-request, 
 resp
@@ -86,62 +86,8 @@
 ) %7B%0A
-        if (request.url === '/favicon.ico') %7B%0A        
     
@@ -140,32 +140,24 @@
 t/html' %7D);%0A
-        
     response
@@ -168,352 +168,54 @@
 ();%0A
-            return;%0A        %7D%0A%0A        var filename;%0A        if (request.url === '/') filename = 'index.html';%0A        else filename = '.' + request.url;%0A%0A        if (filename.indexOf('second') !== -1) %7B%0A            try %7B%0A                setTimeout(function() %7B%0A                    var filecontent = fs.readFileSync(filename);%0A%0A                
+%7D;%0A%0Avar sendFile = function(response, file) %7B%0A
     
@@ -272,32 +272,16 @@
 ml' %7D);%0A
-                
     resp
@@ -299,46 +299,146 @@
 file
-content);%0A                    
+);%0A    response.end();%0A%7D;%0A%0Avar sendFileWithDelay = function(response, file, delay) %7B%0A    setTimeout(function() %7B%0A        sendFile(
 response
 .end
@@ -437,251 +437,367 @@
 onse
-.end(
+, file
 );%0A    
- 
-           %7D, 5000);%0A            %7D catch (e) %7B%0A            %7D%0A        %7D else %7B%0A            try %7B%0A                var filecontent = fs.readFileSync(filename);%0A%0A                response.writeHead(200, %7B 'Content-Type': 'text/html' %7D)
+%7D, delay);%0A%7D;%0A%0Avar transformUrlToPath = function(url) %7B%0A    return url.replace(/%5C//g, '%5C%5C').substring(1);%0A%7D;%0A%0Avar getFile = function(url) %7B%0A    var path = transformUrlToPath(url);%0A%0A    if (path === '') path = 'index.html';%0A%0A    if (!fs.existsSync(path)) %7B%0A        path = '..%5C%5C' + path;%0A%0A        if (!fs.existsSync(path)) %7B%0A            return null
 ;%0A  
@@ -806,105 +806,132 @@
     
+%7D%0A
     
+%7D%0A%0A
     
-response.write(filecontent);%0A                response.end();%0A            %7D catch (
+console.log(path);%0A%0A    return fs.readFileSync(path);%0A%7D;%0A%0Ahttp%0A    .createServer(function (request, respons
 e) %7B
- %7D%0A
 %0A   
@@ -939,30 +939,27 @@
     
-   
+var
  file
-name = '..' + 
+ = getFile(
 requ
@@ -965,16 +965,17 @@
 uest.url
+)
 ;%0A%0A     
@@ -981,15 +981,17 @@
     
-    try
+if (file)
  %7B%0A 
@@ -1005,174 +1005,131 @@
     
-    var filecontent = fs.readFileSync(filename);%0A%0A                response.writeHead(200, %7B 'Content-Type': 'text/html' %7D);%0A                response.write(filecontent
+if (request.url.indexOf('second') !== -1) sendFileWithDelay(response, file, 5000);%0A            else sendFile(response, file
 );%0A 
@@ -1135,24 +1135,26 @@
         
+%7D%0A
         
 response
@@ -1149,61 +1149,31 @@
     
-response.end();%0A            %7D catch (e) %7B %7D%0A        %7D
+else send404(response);
 %0A   

refactor demo env server