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
OK, i'm so new to this and it shows, thanks everyone for helping me I have now found the cause. I was stupid and did not mention i'm on windows, I thought this was irrelevant as I had no idea this code was making a external call to another program.
Yeah I know, stupid Joe, I thought tail was just a command within node and didn't think properly. This mess makes sense now.
var tail_child = spawn('C:/cygwin/bin/tail.exe', ['-f', 'var/log/system.log']);
This fixes everything, install cygwin and hardlink that binary! This code was meant for a linux/unix environment I guess, so we just have to make do.
I was talking with the author of nodetuts, (Pedro) and I mentioned I was on a windows box, Bam it all made sense, the code makes more sense to me now too (I was searching for ages on node documentation for -f
and couldn't find much. haha)
I should have put windows in the description of my environment.
Oh well.
Silly Joe.
Hy Joseph, I recomend to you to change this line in your code to see what kind of error do you have
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);
});
/* Add this line to see the error in your terminal */
tail_child.stderr.pipe(process.stdout);
}).listen(4000);
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.