node.js fs.createReadStream() 等效于 Node 中的远程文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14544911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:01:09  来源:igfitidea点击:

fs.createReadStream() equivalent for remote file in Node

node.js

提问by Aaron Moodie

Is there an equivelate method to fs.createReadStream() in Node for remote files? Using as follows throws Unhandled 'error' event

Node 中的远程文件是否有与 fs.createReadStream() 等效的方法?使用如下抛出Unhandled 'error' event

var s = fs.createReadStream('some_mp3_url');

回答by Ricardo Tomasi

Node is no PHP :)

Node 不是 PHP :)

Use the requestmodule:

使用请求模块:

request('http://fromrussiawithlove.com/baby.mp3').pipe(fs.createWriteStream('song.mp3'))

回答by smhg

It could be better to use a module like request(alternatives), but you could do it like this:

使用像request替代品)这样的模块可能会更好,但你可以这样做:

ES6 version

ES6版本

http.get('some_mp3_url', res => res.pipe(fs.createWriteStream('some.mp3')));

ES5 version

ES5版本

http.get('some_mp3_url', function (res) {
  res.pipe(fs.createWriteStream('some.mp3'));
});

Note: besides fs, the http(or https) module also has to be imported/required.

注意:除此之外fs,还必须导入/需要http(or https) 模块。