Skip to content

Instantly share code, notes, and snippets.

@thbley
Last active December 12, 2015 06:09
Show Gist options
  • Save thbley/4727187 to your computer and use it in GitHub Desktop.
Save thbley/4727187 to your computer and use it in GitHub Desktop.
Run tail -f foobar.log inside a browser with Nodejs in the backend.
var ip = "127.0.0.1";
var port = 8000;
var filename = "/ramdisk/access.log";
var tail = require("child_process").spawn("tail", ["-f", filename]);
require("http").createServer(function(req, res){
if (req.url=="/tail") {
res.writeHead(200, {"Content-Type": "text/plain;charset=UTF-8"});
res.write("Running tail -f "+filename+" ...\n");
tail.stdout.on("data", function(data){
res.write(data);
});
} else {
res.writeHead(200, {"Content-Type": "text/html;charset=UTF-8"});
var out = "<html>\n<body>\n\
<script>\n\
var xhr = new XMLHttpRequest();\n\
xhr.open('GET', 'http://"+ip+":"+port+"/tail');\n\
xhr.onreadystatechange = function(){\n\
document.body.innerHTML = '<pre>'+xhr.responseText.replace(/</g, '&lt;')+'</pre>';\n\
window.scrollTo(0, document.body.scrollHeight);\n\
};\n\
xhr.send();\n\
</script></body></html>";
res.end(out);
}
}).listen(port, ip);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment