var sys = require('sys'), http = require('http'), fs = require('fs'), url = require('url'), events = require('events'); function main() { new HttpServer({ 'GET': (function() { var servlet = new StaticServlet(); return servlet.handleRequest.bind(servlet) })() }).start(8000); } function escapeHtml(value) { return value.toString(). replace('<', '<'). replace('>', '>'). replace('"', '"'); } /** * An Http server implementation that uses a map of methods to decide * action routing. * * @param {Object} Map of method => Handler function */ function HttpServer(handlers) { this.handlers = handlers; this.server = http.createServer(this.handleRequest_.bind(this)); } HttpServer.prototype.start = function(port) { this.port = port; this.server.listen(port); sys.puts('Http Server running at http://127.0.0.1:' + port + '/'); }; HttpServer.prototype.parseUrl_ = function(urlString) { var parsed = url.parse(urlString); parsed.pathname = url.resolve('/', parsed.pathname); return url.parse(url.format(parsed), true); }; HttpServer.prototype.handleRequest_ = function(req, res) { sys.puts(req.method + ' ' + req.url); req.url = this.parseUrl_(req.url); var handler = this.handlers[req.method]; if (!handler) { res.writeHead(501, 'Not Implemented'); res.end(); } else { handler.call(this, req, res); } }; /** * Handles static content. */ function StaticServlet() {} StaticServlet.MimeMap = { 'txt': 'text/plain', 'html': 'text/html', 'css': 'text/css', 'xml': 'application/xml', 'json': 'application/json', 'js': 'application/javascript', 'jpg': 'image/jpeg', 'jpeg': 'image/jpeg', 'gif': 'image/gif', 'png': 'image/png' }; StaticServlet.prototype.handleRequest = function(req, res) { var self = this; var path = ('./' + req.url.pathname).replace('//','/'); var parts = path.split('/'); if (parts[parts.length-1].charAt(0) === '.') return self.sendForbidden_(res, path); fs.stat(path, function(err, stat) { if (err) return self.sendMissing_(res, path); if (stat.isDirectory()) return self.sendDirectory_(res, path); return self.sendFile_(res, path); }); } StaticServlet.prototype.sendError_ = function(res, error) { res.writeHead(500, 'Internal Server Error', { 'Content-Type': 'text/html' }); res.write('\n'); res.write('
' + escapeHtml(sys.inspect(error)) + ''); sys.puts('500 Internal Server Error'); sys.puts(sys.inspect(error)); }; StaticServlet.prototype.sendMissing_ = function(res, path) { res.writeHead(404, 'Not Found', { 'Content-Type': 'text/html' }); res.write('\n'); res.write('