如何将STDIN传递给node.js子进程 [英] How to pass STDIN to node.js child process

查看:95
本文介绍了如何将STDIN传递给node.js子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个包装了 pandoc 节点的库。但是我不知道如何将STDIN传递给子进程ʻexecFile ...

I'm using a library that wraps pandoc for node. But I can't figure out how to pass STDIN to the child process `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);
});

在CLI上看起来像这样:

On the CLI it would look like this:

echo "# Hello World" | pandoc -f markdown -t html

UPDATE 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?


推荐答案

这是我的工作方式:

var cp = require('child_process');
var optipng = require('pandoc-bin').path; //This is a path to a command
var child = cp.spawn(optipng, ['--from=markdown', '--to=html']); //the array is the arguments

child.stdin.write('# HELLO'); //my command takes a markdown string...

child.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});
child.stdin.end();

这篇关于如何将STDIN传递给node.js子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆