Skip to content

Instantly share code, notes, and snippets.

@r
Last active August 29, 2015 13:59
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 r/10802355 to your computer and use it in GitHub Desktop.
Save r/10802355 to your computer and use it in GitHub Desktop.
var http = require("http");
var url = require("url");
function writeResponse(response, code, text) {
response.writeHead(code, {"Content-Type": "text/plain"});
response.write(text);
response.end();
}
function index(response) {
writeResponse(response, 200, "Hello World");
}
function index2(response) {
writeResponse(response, 200, "Hi World");
}
routingTable = {}
routingTable['/index'] = index;
routingTable['/index2'] = index2;
function route(pathname, response) {
console.log("Incoming request -- " + pathname);
if (typeof routingTable[pathname] === 'function') {
return routingTable[pathname](response);
} else {
console.log(pathname + " == 404");
writeResponse(response, 404, "404 Not found");
}
}
http.createServer(function(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Received request for " + pathname);
route(pathname, response);
}).listen(8888);
console.log("Server started.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment