index.js
function saveUrlToFile(url, filePath) {
  return new Promise((resolve, reject) => {
    const outFile = fs.createWriteStream(filePath);
    const protocol = url.startsWith('https') ? https : http;
    protocol.get(url, (response) => {
      response.pipe(outFile);
      response.on('end', () => resolve(filePath));
      response.on('error', (err) => {
        // outFile.destroy();
        // fs.unlinkSync(filePath);
        reject(err);
      });
    })
      .on('error', (err) => {
        reject(err);
      });
  });
}
function saveUrlToFile(url, filePath) {
  return new Promise((resolve, reject) => {
    const outFile = fs.createWriteStream(filePath);
    const protocol = url.startsWith('https') ? https : http;
    protocol.get(url, (response) => {
      response.pipe(outFile);
      response.on('end', () => resolve(filePath));
      response.on('error', (err) => {
        // outFile.destroy();
        // fs.unlinkSync(filePath);
        reject(err);
      });
    })
      .on('error', (err) => {
        reject(err);
      });
  });
}
function saveUrlToFile ( url , filePath ) { return new Promise ( ( resolve , reject ) => { const outFile = fs . createWriteStream ( filePath ) ; const protocol = url . startsWith ( 'https' ) ? https : http ; protocol . get ( url , ( response ) => { response . pipe ( outFile ) ; response . on ( 'end' , ( ) => resolve ( filePath ) ) ; response . on ( 'error' , ( err ) => { // outFile.destroy(); // fs.unlinkSync(filePath); reject ( err ) ; } ) ; } ) . on ( 'error' , ( err ) => { reject ( err ) ; } ) ; } ) ; }
Save the content of a url to a file
@param {string} url where will be done an HTTP/GET to get the content
@param {string} filePath path and filename where store the output of url
@returns {Promise<string>} resolve with the filePath saved
Save the content of a url to a file