Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
Cómo asegurarse de que realmente hay un archivo en un enlace antes de intentar crearWriteStream

Quiero estar seguro de que realmente hay un archivo para descargar antes de usar fs.createWriteStream porque, tal como está ahora, descarga una página 404 y la guarda como un archivo xls...

He escrito el siguiente código. ¿Cómo puedo implementar eso y, en caso de falla, quiero poder enviar un mensaje a 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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!