本文介绍了来自child_process.spawn curl请求的流式响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run the cURL command to install RVM and ruby via child_process.spawn, but it always errors out:

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修饰版本是:

The JSON-prettified version, without the stacktrace, is:

{
    "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 效果很好,但是我希望能够流输出。

It works fine if I use child_process.exec, but I'd like to be able to stream the output.

推荐答案

child_process.spawn()应该传递要运行的命令的名称以及列表其中的论点。

child_process.spawn() should get passed the name of a command to run, and a list of it's arguments. You're feeding it a shell pipeline.

要使其正常工作,您需要运行一个shell并将该管道作为参数传递:

For that to work, you need to run a shell and pass the pipeline as an argument:

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

这篇关于来自child_process.spawn curl请求的流式响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 06:30