假设我有1.js
:
process.stdout.write("Hello world.\n")
2.js
:
process.stdout.write(
require("fs")
.readFileSync(process.argv[2])
.toString()
.replace(/Hello/,"Goodbye")
)
并且可以在bash终端中执行以下命令以获得2.js
输出:
$ node 2.js <(node 1.js)
Goodbye world.
如何在没有bash
调解的情况下模拟相同的流程替换机制?我应该在3.js
写一下什么?
var child_process = require("child_process")
var cp1 = child_process.spawn("node",[__dirname + "/1.js"])
var dev_fd_pipe = ??? // How do I create it?
var cp2 = child_process.spawn("node",[__dirname + "/2.js",dev_fd_pipe])
cp2.stdout.pipe(process.stdout)