fs.createReadStream() equivalent for remote file in Node

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.


Node is no PHP :)

Use the request module:

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

Tags:

Node.Js