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
fs.createReadStream() equivalent for remote file in Node
提问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
回答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) 模块。

