Forked from moreindirection/snap-benchmark's pong implementation in node.js
Created
January 23, 2011 01:50
-
-
Save xk/791723 to your computer and use it in GitHub Desktop.
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
function server (request, response) { | |
var uri = url.parse(request.url).pathname; | |
if(uri=='/pong') { | |
response.writeHead(200, {'Content-Type': 'text/plain'}); | |
response.end('PONG'); | |
} else if ((match = uri.match(/^\/echo\/(.*)$/)) != null) { | |
response.writeHead(200, {'Content-Type': 'text/plain'}); | |
response.end(match[1]); | |
} else { | |
var filename = path.join(process.cwd(), uri); | |
path.exists(filename, pathExistsCB); | |
} | |
function pathExistsCB (exists) { | |
if (exists) { | |
fs.readFile(filename, "binary", readFileCB); | |
} | |
else { | |
response.writeHead(404, {"Content-Type": "text/plain"}); | |
response.end("404 Not Found\n"); | |
} | |
} | |
function readFileCB (err, file) { | |
if (err) { | |
response.writeHead(500, {"Content-Type": "text/plain"}); | |
response.end(err + "\n"); | |
return; | |
} | |
response.writeHead(200); | |
response.write(file, "binary"); | |
response.end(); | |
} | |
} | |
http.createServer(server).listen(8124, "localhost"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment