I am refactoring some code that was using http
module in Node to use got
instead. I tried the following:
function get(url, filePath) {
return new Promise((resolve, reject) => {
got.stream(url).on
("response", response => {
const newFile = fs.createWriteStream(filePath);
response.pipe(newFile);
newFile.on("finish", () => {
newFile.close(resolve());
});
newFile.on("error", err => {
reject(err);
});
}).on
("error", err => {
reject(err);
});
});
}
The finish
event never fired. The file (filePath
) is created with 0 bytes.
The block of code using newFile
was something that worked when I was using the Node http
module.
What is the proper way to pipe got.stream
to a file?