2012-04-28 13 views
0

child_processモジュール経由でコマンドを実行しようとしましたが、次のようにクライアントブラウザに結果を返します。console.log(stdout)が見つかりましたが、response.write()は機能しません。上記のコードchild_process execがクライアントのブラウザに応答できません

function start(response) { 
    dir_list(function(stdout) { 
     response.writeHead(200, {"Content-Type": "text/plain"}); 
     response.write(stdout); 
     console.log(stdout); 
     response.end(); 
});  

function dir_list(callback) { 
    var exec = require('child_process').exec 
    child = exec('ls -la', function(error, stdout, stderr) { 
     callback(stdout); 
    }); 
} 

私は Node.js: Object value as stdout of child_process.execを参照している私がrefractored、しかし、それは私のために動作しませんunlucklyれます。

ところで、私の元々のコードは次のとおりです。

function start(response) { 
     console.log("Request handler 'start' was called."); 
     var exec = require('child_process').exec; 
     exec("ls -lah", function (error, stdout, stderr) { 
     response.writeHead(200, {"Content-Type": "text/plain"}); 
     response.write(stdout); 
     console.log(stdout); 
     response.end(); 
}); 
+0

どういう意味ですか?応答はクライアント側で受信されていますか?エラーメッセージはありますか? – supertopi

+0

元のコードは機能しましたか? – Eloff

+0

@superopiそれは私のブラウザで何も得られないことを意味しません。それは有線だ "console.log(stdout);"つまり、コンソールでログ情報を取得できます。 – Weibo

答えて

1

あなたのコードはstart関数の最後に近い}が不足しています。

function start(response) { 
    dir_list(function(stdout) { 
     response.writeHead(200, {"Content-Type": "text/plain"}); 
     response.write(stdout); 
     console.log(stdout); 
     response.end(); 
    }); 
} 

function dir_list(callback) { 
    var exec = require('child_process').exec 
    child = exec('ls -la', function(error, stdout, stderr) { 
     callback(stdout); 
    }); 
} 
+1

あなたの情報をありがとう、はい、 '}'私は私のコードをStackoverflowにコピーすると私によって失われます。根本的な原因は、以下のようにstart()メソッドを呼び出す前にresponse.end()を呼び出すことです。 router.route(handle、pathName、response); response.end(); ; solutinはちょうど "reponse.end()"を削除しています – Weibo

+0

@Weiboこれを回答として投稿することができます。 –

+0

私はこのようなことをしばらく働こうとしていましたが、私はこれを見つけました...ありがとう...ありがとう...ありがとう。あなたはスーパーです★ –

関連する問題