Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.