简体   繁体   English

如何让用户在Node.js上生成的数据中输入数据

[英]How to let user type in data into a spawned process on Node.js

I have a program I run programmatically on Node.js using child_process. 我有一个使用child_process在Node.js上以编程方式运行的程序。

var spawnedProcess = childProcess.spawn(command, args);

spawnedProcess.stdout.setEncoding("utf8");
spawnedProcess.stdout.on("data", function(data) {
  return process.stdout.write(data);
});

process.stdin.pipe(spawnedProcess.stdin);

I thought this would work since everything I type into the shell would be piped into the stdin of the spawned process. 我以为这样会行得通,因为我在shell中键入的所有内容都将通过管道传递到生成的进程的stdin中。

I can see what I am typing and when I hit Enter it just give me a new line. 我可以看到输入的内容,当我按Enter键时,请给我换行。 The shell isn't responding to any input. 外壳不响应任何输入。

Any clue? 有什么线索吗?

As mentioned in the comments, you can potentially actually use both npm and jitsu both can potentially be used directly as modules. 如评论中所述,您实际上可以同时使用npm和jitsu,它们都可以直接用作模块。 You may want to consider that as an option. 您可能希望将其作为一种选择。

When you run them from the command line, all you are doing is running these two scripts: 从命令行运行它们时,您正在做的是运行以下两个脚本:

To answer your question though, it looks totally fine except for one bit. 不过,要回答您的问题,除了一点点,它看起来都很好。 You need to resume the stdin stream before it can be piped, as mentioned in it's docs Docs for process.stdin 您需要先恢复stdin流,然后才能对其进行管道传输,如process.stdin的文档Docs中所述

You just need to add this somewhere around where you call 'pipe'. 您只需将其添加到称为“管道”的位置。

process.stdin.resume()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM