Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda