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

Issue

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.

Solution

You need to recreate csvStream for each new request:

const csvFromDatabase = (data, res) => {
  const csvStream = format({ delimiter: ";", headers: true });
  
  csvStream.pipe(res);
  …
};

Answered By – robertklep

Answer Checked By – Mary Flores (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.