Last active
August 29, 2015 13:59
-
-
Save r/10802355 to your computer and use it in GitHub Desktop.
This file contains 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
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