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

321
Vistas
Why downloading a file from node.js server multiple times results in empty files

I am glad to get some help.

Here is my problem:

I have built a web server with node.js that should send a csv file to the client when requested through a certain route. The csv file is created from json using the fast-csv package. The json data comes from a mongoDB and is processed with mongoose. When I request this route once, it works fine. However, when it is requested a second time, an empty file is sent to the client. By the way, the headers reach the client correctly. I have to restart the server to download my file again.

What did I try:

Basically, I have now lost track of everything I have tried. This behavior occurs both when using postman and when querying via the browser. I've tried implementing promises in my handler function. I've tried to unsubscribe res somehow (but yes, that was a stupid approach). I`ve tried to write the file into the fs and to send it on a second request. ...

Maybe one of you can tell what's going wrong here at first glance:

const { format } = require("@fast-csv/format");
const csvStream = format({ delimiter: ";", headers: true });

const router = express.Router();

router.route("/:collection/csv").get(requireModel, createCsv);

const csvFromDatabase = (data, res) => {
  csvStream.pipe(res);

  const processData = (data) => {
    data.forEach((row) => {
      const { _id, __v, ...newRow } = row._doc;
      csvStream.write({ ...newRow });
    });
    csvStream.end();
  };
  processData(data);
};

const createCsv = async (req, res) => {
  const { model } = req;
  const items = await model.find({});
  res.setHeader("Content-disposition", "attachment; filename=file.csv");
  res.setHeader("Content-type", "text/html; charset=UTF-8");
  csvFromDatabase(items, res);
};

Thank you very much for your help. I hope I didn't bore you with too stupid questions.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to recreate csvStream for each new request:

const csvFromDatabase = (data, res) => {
  const csvStream = format({ delimiter: ";", headers: true });
  
  csvStream.pipe(res);
  …
};
about 4 years ago · Juan Pablo Isaza Denunciar
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