To read remote file with Node.js http.get, we create a write stream.
For instance, we write
const fs = require("fs");
const https = require("https");
const file = fs.createWriteStream("data.txt");
https.get("https://www.w3.org/TR/PNG/iso_8859-1.txt", (response) => {
const stream = response.pipe(file);
stream.on("finish", () => {
console.log("done");
});
});
to call createWriteStream
to create a write stream to write to data.txt.
Then we call get
to make a get request to the file.
We get the response
as a read stream and call pipe
to pipe it to the file
write stream.
And we call on
to listen to the finish
event which is emitted when writing is done.