Last active
December 12, 2015 06:09
-
-
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.
This file contains 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
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, '<')+'</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