Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js, nodetuts tutorial - example code will run, timeout after a while,(Chunked HTTP encoding)

Node.js, nodetuts tutorial - example code will run, timeout after a while,(Chunked HTTP encoding)

Problem

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
Problem courtesy of: Joseph

Solution

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.

Solution courtesy of: MiguelSanchezGonzalez

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js, nodetuts tutorial - example code will run, timeout after a while,(Chunked HTTP encoding)

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×