Nodejssubprocess:从已经初始化的进程写入stdin

我正尝试使用节点的child_process产生一个外部进程phantomjs ,然后在初始化之后将信息发送到该进程,这可能吗?

我有以下代码:

 var spawn = require('child_process').spawn, child = spawn('phantomjs'); child.stdin.setEncoding = 'utf-8'; child.stdout.pipe(process.stdout); child.stdin.write("console.log('Hello from PhantomJS')"); 

但是我在stdout上唯一得到的是phantomjs控制台的初始提示。

 phantomjs> 

所以似乎child.stdin.write没有任何效果。

我不知道我可以发送额外的信息phantomjs最初的产卵。

提前致谢。

您还需要传递\n符号才能使您的命令正常工作:

 var spawn = require('child_process').spawn, child = spawn('phantomjs'); child.stdin.setEncoding('utf-8'); child.stdout.pipe(process.stdout); child.stdin.write("console.log('Hello from PhantomJS')\n"); child.stdin.end(); /// this call seems necessary, at least with plain node.js executable 
Interesting Posts