如何将 STDIN 传递给 node.js 子进程
How to pass STDIN to node.js child process
我正在使用一个为节点包装 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);
});
在 CLI 上它看起来像这样:
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?
根据这些 docs 和以下摘录,我不确定是否可以将 STDIN
与 child_process.execFile()
一起使用,看起来它仅适用于 child_process.spawn()
The child_process.execFile() function is similar to child_process.exec() except that it does not spawn a shell. Rather, the specified executable file is spawned directly as a new process making it slightly more efficient than child_process.exec().
这是我如何让它工作的:
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();
喜欢spawn()
, execFile()
also returns a ChildProcess
instance which has a stdin
可写流。
作为 write()
and listening for the data
event, you could create a readable stream, push()
your input data, and then pipe()
的替代方法 child.stdin
:
var execFile = require('child_process').execFile;
var stream = require('stream');
var optipng = require('pandoc-bin').path;
var child = execFile(optipng, ['--from=markdown', '--to=html'], function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
});
var input = '# HELLO';
var stdinStream = new stream.Readable();
stdinStream.push(input); // Add data to the internal queue for users of the stream to consume
stdinStream.push(null); // Signals the end of the stream (EOF)
stdinStream.pipe(child.stdin);
作为标准输入的字符串
如果您使用同步方法(execFileSync
、execSync
或 spawnSync
),您可以使用input
输入选项。像这样:
const child_process = require("child_process");
const str = "some string";
const result = child_process.spawnSync("somecommand", ["arg1", "arg2"], { input: str });
我正在使用一个为节点包装 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);
});
在 CLI 上它看起来像这样:
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?
根据这些 docs 和以下摘录,我不确定是否可以将 STDIN
与 child_process.execFile()
一起使用,看起来它仅适用于 child_process.spawn()
The child_process.execFile() function is similar to child_process.exec() except that it does not spawn a shell. Rather, the specified executable file is spawned directly as a new process making it slightly more efficient than child_process.exec().
这是我如何让它工作的:
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();
喜欢spawn()
, execFile()
also returns a ChildProcess
instance which has a stdin
可写流。
作为 write()
and listening for the data
event, you could create a readable stream, push()
your input data, and then pipe()
的替代方法 child.stdin
:
var execFile = require('child_process').execFile;
var stream = require('stream');
var optipng = require('pandoc-bin').path;
var child = execFile(optipng, ['--from=markdown', '--to=html'], function (err, stdout, stderr) {
console.log(err);
console.log(stdout);
console.log(stderr);
});
var input = '# HELLO';
var stdinStream = new stream.Readable();
stdinStream.push(input); // Add data to the internal queue for users of the stream to consume
stdinStream.push(null); // Signals the end of the stream (EOF)
stdinStream.pipe(child.stdin);
作为标准输入的字符串
如果您使用同步方法(execFileSync
、execSync
或 spawnSync
),您可以使用input
输入选项。像这样:
const child_process = require("child_process");
const str = "some string";
const result = child_process.spawnSync("somecommand", ["arg1", "arg2"], { input: str });