fs.createReadStream() equivalent for remote file in Node

后端 未结 2 1003
面向向阳花
面向向阳花 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 15:47

    Node is no PHP :)

    Use the request module:

    request('http://fromrussiawithlove.com/baby.mp3').pipe(fs.createWriteStream('song.mp3'))
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题