node js与shell应用程序交互

时间:2021-07-16 21:41:45

There are plenty of node js examples on online about how to spawn a child process and then catch the result as a string for your own processing.

网上有很多关于如何生成子进程的节点js示例,然后将结果作为字符串捕获到您自己的处理中。

But...

I want to 'interact' with a child process. For example, how would I write a node js application than starts by calling 'python' and then types a statement '1+1', lets me catch the result '2', before proceeding to type another arbitrary statement '4+4'?

我希望与子进程“互动”。例如,如何编写节点js应用程序而不是通过调用'python'然后键入语句'1 + 1',让我捕获结果'2',然后再继续键入另一个任意语句'4 + 4' ?

(And by 'type' I'm assuming it will require streaming data to the stdin that the process uses).

(并且通过'type'我假设它将需要流数据到该进程使用的stdin)。

1 个解决方案

#1


7  

var child = require('child_process');
var ps = child.spawn('python', ['-i']);
ps.stdout.pipe(process.stdout);
ps.stdin.write('1+1');
ps.stdin.end();

works a treat!

有效!

#1


7  

var child = require('child_process');
var ps = child.spawn('python', ['-i']);
ps.stdout.pipe(process.stdout);
ps.stdin.write('1+1');
ps.stdin.end();

works a treat!

有效!