This is a node tutorial for a beginner. Why doesn’t my code work? It’s identical to his and I have tried workarounds without success. Please help if you can.
http://nodetuts.com/tutorials/2-webtail-nodejs-child-processes-and-http-chunked-encoding.html#video
In this tutorial, he writes a log/text file to the webpage located at localhost:4000, previous examples work (tutorial 1) however I can’t get this to do anything at all, it runs and that’s all.
Could anyone get this printing a file to the webpage please. 🙂
Thanks!
var http = require('http');
var spawn = require('child_process').spawn;
http.createServer(function(request, response){
response.writeHead(200, {
'Content-Type' : 'text/plain'
});
var tail_child = spawn('tail', ['-f', '/var/log/system.log']);
request.connection.on('end', function(){
tail_child.kill();
});
tail_child.stdout.on('data', function(data){
console.log(data.toString());
response.write(data);
});
}).listen(4000);
I have tried the following and they are making no difference:
console.log(data.toString());
response.write(data);
response.end();
and
console.log(data.toString());
response.end(data);
Edit after MiguelSanchezGonzalez answer:
I added tail_child.stderr.pipe(process.stdout); into the right place, and this is the response I am getting:
CreateProcessW: The system cannot find the file specified.
A file certainly does exist here, and I have also tested many different paths including text files in the same folder as the script (such as '/test.txt' for example. So maybe i’m wrong, i just assumed when it said file it was talking about my file.
Edit 2:
I also checked if the path exists (crudely built from here): Fastest way to check for existence of a file in NodeJs and it does say that the file indeed exists.
This code:
var file = path.normalize = ('var/log/system.log');
fs.exists(file, function(exists){
util.debug(exists ? "yep, its there":"nope");
});
console.log(file);
outputs this:
var/log/system.log
DEBUG: yep, its there
Hy Joseph, I recomend to you to change this line in your code to see what kind of error do you have
This will show you the description of the error, maybe you don’t have this file in your filesystem o you can’t open it, try changing the path file to other that you know that exists and can open it.