【发布时间】:2013-04-27 07:31:48
【问题描述】:
我了解了如何使用 Node.js 流将内容传送到一起,但是鉴于其中一些脚本可以是异步的,如何使用 Unix | 将多个脚本传送到一起?
$ ./a.js | ./b.js
例子:
a.js (chmod 0755)
#!/usr/bin/env node
setTimeout(function(){
console.log(JSON.stringify({ foo: 'bar' }));
}, 10);
b.js (chmod 0755)
#!/usr/bin/env node
console.log(process.argv);
这是输出:
$ ./a.js | ./b.js
[ 'node', '/Users/viatropos/tests/b.js' ]
events.js:72
throw er; // Unhandled 'error' event
^
Error: write EPIPE
at errnoException (net.js:883:11)
at Object.afterWrite (net.js:700:19)
乍一看似乎有很多问题,所以不确定从哪里开始。有没有办法让它工作?最终目标是能够从./a.js 获取console.log 输出并在./b.js 中使用它。原因是,大多数情况下,这些脚本一次运行一个,但有时能够将它们通过管道连接在一起会很好,因此理想情况下,系统应该能够处理这两种情况。
【问题讨论】:
标签: javascript node.js unix pipe command-line-interface