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 7sharp9/1063336 to your computer and use it in GitHub Desktop.
Save 7sharp9/1063336 to your computer and use it in GitHub Desktop.
http.createServer(function (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, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.end("404 Not Found\n");
return;
}
fs.readFile(filename, "binary", function(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();
});
});
}
}).listen(8124, "localhost");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment