2015-04-28 23 views
7

我在寫一些node.js腳本來啓動子進程。代碼片段如下。NodeJS子進程stdout都是數字

var spawn = require('child_process').spawn; 
var child = spawn ('node', ['script.js']) 

child.stdout.on('data', 
    function (data) { 
     logger.verbose('tail output: ' + JSON.stringify(data)); 
    } 
); 

child.stderr.on('data', 
    function (data) { 
     logger.error('err data: ' + data); 
    } 
); 

腳本運行良好,除了子進程的輸出和錯誤只打印輸出數值:

輸出示例:

108,34,44,34,105,110,99,114,101,97,109,101,110,116,97,108,95,112,111,108,108,105 

如何將這些數值轉換爲可讀的字符串?

謝謝。

+0

嘗試使用data.toString('utf8'); http://stackoverflow.com/questions/12121775/convert-streamed-buffers-to-utf8-string – vanadium23

回答

5

data是一個數組緩衝區。調用它的toString方法JSON.stringify(data.toString('utf8'))

+0

完美!謝謝,穆格斯。 – Lee