我正在使用一个node.js中包装pandoc
的库。但是我无法弄清楚如何将STDIN传递给子进程`execFile...
var execFile = require('child_process').execFile;
var optipng = require('pandoc-bin').path;
// STDIN SHOULD GO HERE!
execFile(optipng, ['--from=markdown', '--to=html'], function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
});
在命令行界面上,它看起来像这样:
echo "# Hello World" | pandoc -f markdown -t html
更新 1
尝试使用spawn
让它工作:
var cp = require('child_process');
var optipng = require('pandoc-bin').path;
var child = cp.spawn(optipng, ['--from=markdown', '--to=html'], { stdio: [ 0, 'pipe', 'pipe' ] });
child.stdin.write('# HELLO');
// then what?
send()
函数从子进程发送消息到父进程,但不支持从父进程发送消息到子进程。当我使用fork
而不是spawn
时,它开始双向发送消息。 - Ankur Thakur