Created
December 10, 2010 16:40
-
-
Save jettero/736439 to your computer and use it in GitHub Desktop.
I wanted a way to quickly test some html templates I was barfing up...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jslint white: false, onevar: false, laxbreak: true, maxerr: 500000 | |
*/ | |
/*global require console process | |
*/ | |
var http = require('http'), | |
url = require('url'), | |
fs = require('fs'); | |
function mystat(path) { | |
var stats; | |
try { stats = fs.statSync(path); } catch(ignore){} | |
return stats; | |
} | |
http.createServer(function (req, res) { | |
var parts, stats, file_contents, ext, ct, path = url.parse(req.url, false).pathname.replace(/\.\./g, "dick-move"); | |
if( parts = path.match(/\.([^.]+)$/) ) | |
ext = parts[1]; | |
switch(ext) { | |
case "html": ct = "text/html; charset=utf-8"; break; | |
case "css": ct = "text/css; charset=utf-8"; break; | |
case "ipk": ct = "application/octet-stream"; break; | |
case "js": ct = "application/javascript"; break; | |
} | |
path = "." + (path.match(/^\//) ? path : "/" + path); | |
stats = mystat(path); | |
try { | |
if( !stats ) { | |
console.log("404(" + path + ")"); | |
res.writeHead(404, {'Content-Type': "text/plain"}); | |
res.end("404 ERROR: file not found\n"); | |
} else if( stats.isFile() ) { | |
console.log("200-file(" + path + ")"); | |
file_contents = fs.readFileSync(path); | |
res.writeHead(200, {'Content-Type': ct}); | |
res.end(file_contents); | |
} else if( !process.env.skip_index && stats.isDirectory() && mystat(path + "index.html") ) { | |
console.log("200-file(" + path + "auto(index.html))"); | |
var file_contents = fs.readFileSync(path + "index.html"); | |
res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'}); | |
res.end(file_contents); | |
} else if( stats.isDirectory() ) { | |
console.log("200-dir(" + path + ")"); | |
var files = fs.readdirSync(path); | |
res.writeHead(200, {'Content-type': "text/html; charset=utf-8"}); | |
res.write("<ul>\n"); | |
for(var i=0; i<files.length; i++) | |
res.write("<li><a href='" + files[i] + "'>" + files[i] + "</a></li>\n"); | |
res.end("</ul>\n"); | |
} else { | |
console.log("403(" + path + ")"); | |
res.writeHead(403, {'Content-Type': "text/plain"}); | |
res.end("403 ERROR: permission denied, file type not handled\n"); | |
} | |
} catch(e) { | |
console.log("500(" + path + "): " + e); | |
res.writeHead(500, {'Content-Type': "text/plain; charset=utf-8"}); | |
res.end("500 ERROR: " + e + " — loading(" + path + ", " + ext + ")\n"); | |
} | |
}).listen(process.argv[2] || 9000); | |
console.log("Server running at http://" | |
+ (process.env.HOSTNAME || "localhost") + ":" | |
+ (process.argv[2] || 9000) + "/" | |
+ (process.env.skip_index ? " — skipping index.html when found" : "")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment