Tail on browser

Might come handy, so here it is. It basically tails /var/log/messages and sends it as a HTTP response chunk encoded.
Nothing fancy - very simple and nice ;-)
 
var http = require("http")
var spawn = require("child_process").spawn;
http.createServer(function(req,res){
    var tail = spawn("tail", ["-f", "/var/log/messages"]);
    res.writeHead(200,{"Content-Type": "text/plain"});
    tail.stdout.on("data", function (chunk) {
        res.write(chunk);
    });
    tail.on("exit", function (status) {
        if (status !== 0) 
        {
              console.error("tail exited with code " + status);
              exit(1);
        }
        res.end();
    });
}).listen(8000);
 
Node (http://nodejs.org) is very wery callback oriented. What you see above as function(...) within evented calls (tail.on(...)) are anonymous callback functions that are executed on those particular event. If you're someone from Java world, you can think of this as listeners that are waiting on a particular events to occur. Someone from non-Java world might already be familiar with callbacks. 
 
Node is very nice - I just hope it continues to grow on server-side use. Comments? xD


Jotted by ishwor Nov. 26, 2010
Bookmark and Share

Comments

No comment posted yet

Trackbacks

No trackbacks yet

Leave a Reply/Discuss







Spam Prevention: To post your comment without previewing it you need to complete the captcha below, type the words you see or hear into the box below. This helps stop spam from being posted to the site.
A Django site. Powered by Python.
Developed using djangle