Skip to content

Instantly share code, notes, and snippets.

@aaronblohowiak
Created March 17, 2010 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronblohowiak/335372 to your computer and use it in GitHub Desktop.
Save aaronblohowiak/335372 to your computer and use it in GitHub Desktop.
HOST = null; // localhost
PORT = 80; //run it as root, numb-nuts!!!
sys = require("sys");
createServer = require("http").createServer;
readFile = require("fs").readFile;
url = require("url");
function Connection(request, response){
if ( !(this instanceof Connection) )
return new Connection(request, response);
this.req = request;
this.res = response;
var parse_data = url.parse(this.req.url, true);
this.path = parse_data.pathname;
this.query = parse_data.query;
return this;
};
Connection.prototype = {
respond: function(status, body, type){
this.res.sendHeader(status, [
["Content-Type", type],
["Content-Length", body.length]
]);
this.res.write(body);
this.res.close();
},
returnJSON: function(obj){
this.respond(200, JSON.stringify(obj), "text/json");
},
returnText : function(s){ this.respond(200, s, 'text/plain');},
notFound : function(s){ this.respond(404, s, 'text/plain');}
};
server = createServer(Connection);
server.listen(PORT, HOST);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment