Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.createReadStream() equivalent for remote file in Node

Tags:

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

var s = fs.createReadStream('some_mp3_url'); 
like image 411
Aaron Moodie Avatar asked Jan 27 '13 06:01

Aaron Moodie


People also ask

What is FS createReadStream?

The function fs. createReadStream() allows you to open up a readable stream in a very simple manner. All you have to do is pass the path of the file to start streaming in. It turns out that the response (as well as the request) objects are streams.

How do I create a readable stream from a remote URL in node?

createReadStream() and follow what it calls you can find that it ends up calling fileURULtoPath(url) which will throw if it's not a file: URL. It would suggest using the got() library to get yourself a readstream from a URL: const got = require('got'); const mp4Url = 'https://www.example.com/path/to/mp4Video.mp4'; app.

Is createReadStream synchronous?

createReadStream() appears to have a synchronous interface. It does not return a promise or accept a callback to communicate back when it's done or to send back some results. So, it appears synchronous from the interface.


1 Answers

Node is no PHP :)

Use the request module:

request('http://fromrussiawithlove.com/baby.mp3').pipe(fs.createWriteStream('song.mp3')) 
like image 145
Ricardo Tomasi Avatar answered Sep 22 '22 00:09

Ricardo Tomasi