Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
How to be sure there is actually a file in a link before trying to createWriteStream

I want to be sure that there is actually a file to be downloaded before using fs.createWriteStream because as it is now, it downloads a 404 page and saves it as a xls file...

I have written the following code. How can I implement that and in case of failure I want to be able to send a message to slack

async function downloadFile() {
  function switchCase(value) {
    switch (value) {
      case '1':
        return 'jan';
      case '2':
        return 'fev';
      case '3':
        return 'mar';
      case '4':
        return 'abr';
      case '5':
        return 'mai';
      case '6':
        return 'jun';
      case '7':
        return 'jul';
      case '8':
        return 'ago';
      case '9':
        return 'set';
      case '10':
        return 'out';
      case '11':
        return 'nov';
      case '12':
        return 'dez';
      default:
        return;
    }
  }

  (async function (url, dest) {
    return new Promise((resolve, reject) => {
      var file = fs.createWriteStream(dest);

      var request = https
        .get(url, function (response) {
          response.pipe(file);

          file.on('finish', async function () {
            resolve(true);
            file.close();
            await closeDB();
          });
        })
        .on('error', async function (err) {
          fs.unlink(dest);
          reject(err);
          await fetch(
            `https://hooks.slack.com/services/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`,
            {
              method: 'post',
              body: JSON.stringify({
                text: `url: ${url}, dest: ${dest}, error: ${err}`,
              }),
              headers: { 'Content-Type': 'application/json' },
            }
          );
        });
    });
  })(
    `https://www.anbima.com.br/informacoes/merc-sec-debentures/arqs/d${
      new Date().getFullYear().toString().slice(2) +
      switchCase((new Date().getMonth() + 1).toString()) +
      (new Date().getDate() - 1).toString().padStart(2, '0')
    }.xls`,
    'd' +
      new Date().getFullYear().toString().slice(2) +
      (new Date().getMonth() + 1).toString() +
      (new Date().getDate() - 1).toString().padStart(2, '0') +
      '.xls'
  );
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda