node.js使用spawn for perl - stdout逐行

时间:2011-07-15 04:03:38

标签: node.js

在nodeJS中生成。我刚刚设法使用它来运行bash命令,如下所示。这似乎非常不集中,我在浏览器屏幕上采取行动,因为命令会破坏数据。

ls = spawn('find',['/'] );
response.writeHead(200, { "Content-Type": "text/plain" });

ls.stdout.on('data', function (data) {
   console.log('stdout: ' + data);
   response.write(data);
});

但是我想运行一个带有多个参数的perl脚本。

ls = spawn('blah.pl',['--argstring here', '--arg blah'] );

Perl脚本只是使用getopts CPAN lib编写获取参数,它使用CPAN期望lib运行一堆东西 - 输出到stdout和stderr如果我有错误但我现在主要关心stdout。

事情是这没有给我输出。似乎至少在程序完成执行之前完全阻塞......而且这种情况至少在10分钟内没有。

我使用spawn是错误的吗?

1 个答案:

答案 0 :(得分:1)

我喜欢节点模块“carrier”

carrier = require "carrier"
childproc = require "child_process"    
find = childproc.spawn "find"
find.stdout.setEncoding "utf8"
linereader = carrier.carry find.stdout
linereader.on "line", (line) -> console.log line
相关问题