3

これは初心者向けのノード チュートリアルです。コードが機能しないのはなぜですか? それは彼と同じであり、私は回避策を試みましたが成功しませんでした。できれば助けてください。

http://nodetuts.com/tutorials/2-webtail-nodejs-child-processes-and-http-chunked-encoding.html#video

このチュートリアルでは、彼は localhost:4000 にある Web ページにログ/テキスト ファイルを書き込みます。前の例 (チュートリアル 1) は機能しますが、これで何も実行できず、実行され、それだけです。

誰でもこのファイルをWebページに印刷できますか。:) ありがとう!

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);

私は次のことを試しましたが、違いはありません。

    console.log(data.toString());
    response.write(data);
    response.end();

    console.log(data.toString());
    response.end(data);

MiguelSanchezGonzalez回答後に編集:

適切な場所に追加tail_child.stderr.pipe(process.stdout);しましたが、これは私が得ている応答です:

CreateProcessW: The system cannot find the file specified.

ファイルは確かにここに存在し、スクリプトと同じフォルダー内のテキストファイルを含む多くの異なるパスもテストしました(たとえば'/test.txt'、たとえば、間違っているかもしれません。ファイル。


編集2:パスが存在するかどうかも確認しました(ここから大雑把に構築されました):NodeJsでファイルの存在を確認する最速の方法で、ファイルが実際に存在すると言います。

このコード:

var file = path.normalize = ('var/log/system.log');

    fs.exists(file, function(exists){
        util.debug(exists ? "yep, its there":"nope");
    });

    console.log(file);

これを出力します:

var/log/system.log
DEBUG: yep, its there
4

2 に答える 2

2

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.

于 2012-07-02T15:52:20.093 に答える
1

ハイジョセフ、コードのこの行を変更して、どのようなエラーがあるかを確認することをお勧めします

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);

これにより、エラーの説明が表示されます。おそらく、ファイルシステムにこのファイルがないか、開くことができません。パスファイルを、存在し、開くことができるとわかっている別のファイルに変更してみてください。

于 2012-07-01T21:56:27.473 に答える