fs.createReadStream() equivalent for remote file in Node

后端 未结 2 1004
面向向阳花
面向向阳花 2020-12-28 15:11

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

var s = fs.cr         


        
2条回答
  •  攒了一身酷
    2020-12-28 16:00

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

    ES6 version

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

    ES5 version

    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.

提交回复
热议问题