Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created September 1, 2010 02:58
Show Gist options
  • Save agmcleod/560155 to your computer and use it in GitHub Desktop.
Save agmcleod/560155 to your computer and use it in GitHub Desktop.
var sys = require("sys"),
http = require("http"),
fs = require('fs'),
path = require('path');
http.createServer(function(request, response) {
var headers = { "Content-Type": "text/html" };
var filename = path.join(process.cwd(), '../work/terminix/knox/index.html');
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, headers);
response.end("<!DOCTYPE html><html><head><title>404 error</title></head><body><h1>404 error</h1></body></html>")
return;
}
else {
fs.readFile(filename, function(err, file_content) {
if(err) {
throw err;
}
response.writeHead(200, headers);
response.end(file_content);
file_content = file_content.toString();
var types = ['.jpg', '.js', '.css', '.png'];
var resources = [];
for(var t = 0; t < types.length; t++) {
var at_end = false, last_index = 0;
var type = types[t];
while(!at_end) {
last_index = file_content.indexOf(type, last_index);
if(last_index == -1) {
at_end = true;
}
else {
var last = 0, has_start = false;
while(!has_start) {
var temp_last = file_content.indexOf('"', last);
if(temp_last == -1) {
last = 0;
has_start = true;
}
else {
if(temp_last > last_index) {
has_start = true;
}
else {
last = temp_last + 1;
}
}
}
last_index += type.length;
resources.push(file_content.substring(last, last_index));
last_index++;
}
}
}
for(var i = 0; i < resources.length; i++) {
var fn = resources[i];
if (fn.indexOf('.jpg') > 0) headers['Content-Type'] = 'image/jpeg';
if (fn.indexOf('.js') > 0) headers['Content-Type'] = 'text/javascript';
if (fn.indexOf('.css') > 0) headers['Content-Type'] = 'text/css';
if (fn.indexOf('.png') > 0) headers['Content-Type'] = 'image/png';
sys.debug('fn: ' + fn);
response.writeHead(200, headers);
response.end(fn);
}
});
}
});
}).listen(8000);
sys.puts("> Running at port 8000");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment