Thursday, 1 June 2017

Node.js tail doesn't work in real time

I use a Node.js child process to run a JAR file that does some extensive computation and prints logs to a .log file. I use the npm tail module to print to console every new line that gets added to the .log file. But the new lines get printed only after the child process finishes execution. What am I doing wrong?

In my index.js:

router.get("/", function(req, res) {
    var tail = new Tail("file.log");
    tail.on("line", function (data) {
        console.log(data);
    }
    var child = require('child_process').spawn('java', ['-jar', 'file.jar']);
    child.on("exit", function () {
        tail.unwatch();
    }
}

Strangely, when I run Get-Content file.log -Wait on Windows Powershell on the side, the logs print in real time to the console on Node.exe too!



via raul

No comments:

Post a Comment