Skip to content

Instantly share code, notes, and snippets.

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 xk/791723 to your computer and use it in GitHub Desktop.
Save xk/791723 to your computer and use it in GitHub Desktop.
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