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

360
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to recreate csvStream for each new request:

const csvFromDatabase = (data, res) => {
  const csvStream = format({ delimiter: ";", headers: true });
  
  csvStream.pipe(res);
  …
};
over 4 years ago · Santiago Trujillo Report
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!