【问题标题】:Streaming response from child_process.spawn curl request来自 child_process.spawn curl 请求的流式响应
【发布时间】:2016-05-14 16:27:19
【问题描述】:

我正在尝试运行 cURL 命令以通过 child_process.spawn 安装 RVM 和 ruby​​,但它总是出错:

let spawnProcess = spawn('\curl -sSL https://get.rvm.io | bash -s stable --ruby')

spawnProcess.stdout.on('data', data => {
    console.log('DATA RECEIVED')
    console.log(data)
})

spawnProcess.stdout.on('close', () => {
    alert('done!')
})

spawnProcess.stderr.on('data', function(){
    console.log('ON DATA')
    console.log(arguments)
})

spawnProcess.on('error', error => {
    console.log('ON ERROR')
    console.log(JSON.stringify(error), error)
})

我收到的错误是:

{"code":"ENOENT","errno":"ENOENT","syscall":"spawn curl -sSL https://get.rvm.io | bash -s stable --ruby","path":"curl -sSL https://get.rvm.io | bash -s stable --ruby","spawnargs":[]} Error: spawn curl -sSL https://get.rvm.io | bash -s stable --ruby ENOENT
    at exports._errnoException (util.js:890:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:182:32)
    at onErrorNT (internal/child_process.js:348:16)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

没有堆栈跟踪的 JSON 美化版本是:

{
    "code": "ENOENT",
    "errno": "ENOENT",
    "syscall": "spawn curl -sSL https://get.rvm.io | bash -s stable --ruby",
    "path": "curl -sSL https://get.rvm.io | bash -s stable --ruby",
    "spawnargs": []
}

如果我使用child_process.exec,它可以正常工作,但我希望能够流式传输输出。

【问题讨论】:

    标签: node.js curl child-process spawn


    【解决方案1】:

    child_process.spawn() 应该传递要运行的命令的名称,以及它的参数列表。您正在为它提供一个 shell 管道。

    为此,您需要运行一个 shell 并将管道作为参数传递:

    let spawnProcess = spawn('/bin/sh', [ '-c', 'curl -sSL https://get.rvm.io | bash -s stable --ruby' ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多