在阅读了 Node.js 子进程的文档后,我很好奇是否可以将缓冲区传递给该进程。
https://nodejs.org/api/child_process.html
对我来说似乎只能传递字符串?如何传递缓冲区或对象?谢谢!
最佳答案
您只能传递 Buffer 或字符串。
var node = require('child_process').spawn('node',['-i']);
node.stdout.on('data',function(data) {
console.log('child:: '+String(data));
});
var buf = new Buffer('console.log("Woof!") || "Osom\x05";\x0dprocess.exit();\x0d');
console.log('OUT:: ',buf.toString())
node.stdin.write(buf);
输出:
OUT:: console.log("Woof!") || "Osom♣";
process.exit();
child:: >
child:: Woof!
child:: 'Osom\u0005'
child:: >
因为 .stdin
是 writable stream .
\x0d
(CR) 是交互模式下的“Enter”模拟。
关于javascript - 将缓冲区传递给 Node.js 子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30937751/