第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用Node.js執(zhí)行命令行二進(jìn)制文件

使用Node.js執(zhí)行命令行二進(jìn)制文件

catspeake 2019-06-23 15:59:22
使用Node.js執(zhí)行命令行二進(jìn)制文件我正在將CLI庫(kù)從Ruby移植到Node.js。在我的代碼中,我在必要時(shí)執(zhí)行幾個(gè)第三方二進(jìn)制文件。我不知道如何在Node中最好地完成這一任務(wù)。下面是Ruby中的一個(gè)示例,其中我調(diào)用prineXML將文件轉(zhuǎn)換為PDF:cmd = system("prince -v builds/pdf/book.html -o builds/pdf/book.pdf")Node中的等效代碼是什么?
查看完整描述

2 回答

?
MYYA

TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊

對(duì)于新版本的Node.js(v8.1.4),事件和調(diào)用與舊版本相似或相同,但鼓勵(lì)使用標(biāo)準(zhǔn)的更新語(yǔ)言特性。例子:

對(duì)于緩沖的、非流格式的輸出(一次性獲得全部輸出),請(qǐng)使用child_process.exec:

const { exec } = require('child_process');exec('cat *.js bad_file | wc -l', (err, stdout, stderr) => {
  if (err) {
    // node couldn't execute the command
    return;
  }

  // the *entire* stdout and stderr (buffered)
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);});

您也可以將其用于承諾:

const util = require('util');const exec = util.promisify(require('child_process').exec);async function ls() {
  const { stdout, stderr } = await exec('ls');
  console.log('stdout:', stdout);
  console.log('stderr:', stderr);}ls();

如果您希望以塊的形式逐步接收數(shù)據(jù)(輸出為流),請(qǐng)使用child_process.spawn:

const { spawn } = require('child_process');const child = spawn('ls', ['-lh', '/usr']);// use child.stdout.setEncoding('utf8'); 
if you want text chunkschild.stdout.on('data', (chunk) => {
  // data from standard output is here as buffers});// since these are streams, you can pipe them elsewherechild.stderr.pipe(dest);
  child.on('close', (code) => {
  console.log(`child process exited with code ${code}`);});

這兩個(gè)函數(shù)都具有同步對(duì)應(yīng)。一個(gè)例子child_process.execSync:

const { execSync } = require('child_process');// stderr is sent to stderr of parent proces
s// you can set options.stdio if you want it to go elsewherelet stdout = execSync('ls');

以及child_process.spawnSync:

const { spawnSync} = require('child_process');const child = spawnSync('ls', ['-lh', '/usr']);console.log('error', child.error);
console.log('stdout ', child.stdout);console.log('stderr ', child.stderr);

注:下面的代碼仍然可以使用,但主要針對(duì)ES5和之前的用戶。

使用Node.js生成子進(jìn)程的模塊在文獻(xiàn)資料(5.0.0節(jié))。若要執(zhí)行命令并將其完整輸出作為緩沖區(qū)獲取,請(qǐng)使用child_process.exec:

var exec = require('child_process').exec;var cmd = 'prince -v builds/pdf/book.html -o builds/pdf/book.pdf';
exec(cmd, function(error, stdout, stderr) {
  // command output is in stdout});

如果您需要在流中處理流程I/O,例如當(dāng)您期望獲得大量的輸出時(shí),請(qǐng)使用child_process.spawn:

var spawn = require('child_process').spawn;var child = spawn('prince', [
  '-v', 'builds/pdf/book.html',
  '-o', 'builds/pdf/book.pdf']);child.stdout.on('data', function(chunk) {
  // output will be here in chunks});// or if you want to send output elsewherechild.stdout.pipe(dest);

如果正在執(zhí)行文件而不是命令,則可能需要使用child_process.execFile,哪些參數(shù)幾乎與spawn,但是有第四個(gè)回調(diào)參數(shù),如exec用于檢索輸出緩沖區(qū)??雌饋?lái)可能有點(diǎn)像這樣:

var execFile = require('child_process').execFile;execFile(file, args, options, function(error, stdout, stderr) {
  // command output is in stdout});

截至v0.11.12,Node現(xiàn)在支持同步。spawnexec..上面描述的所有方法都是異步的,并且具有同步對(duì)應(yīng)。他們的文件可以找到這里..雖然它們對(duì)腳本很有用,但請(qǐng)注意,與異步生成子進(jìn)程所用的方法不同,同步方法不返回ChildProcess.


查看完整回答
反對(duì) 回復(fù) 2019-06-23
  • 2 回答
  • 0 關(guān)注
  • 956 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)