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