1

I am trying to download a requested file from the frontend to a backend server with nodejs. To do it in react I use axios and in nodejs I am using express. The problem is that with my implementation I get the following error:

Error [ERR_STREAM_CANNOT_PIPE]: Cannot pipe, not readable
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.pipe (node:_http_outgoing:987:22)
    at file:///home/marc/Disco/Projects/GymApp/server/routes/trainer.js:249:9
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Emitted 'error' event on ServerResponse instance at:
    at ServerResponse.pipe (node:_http_outgoing:987:8)
    at file:///home/marc/Disco/Projects/GymApp/server/routes/trainer.js:249:9
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 'ERR_STREAM_CANNOT_PIPE'

Here is the axios request:

const downloadTraining = async (id) => {
    const JWT = new ClassJWT();
    const axiosReq = axios.create();
    await JWT.checkJWT();
    axiosReq
      .post(`${serverPath}/download-training`, {
        trainer_id: await JWT.getId(),
        training_id: id,
        token: JWT.getToken(),
      })
      .then((res) => {
        console.log(res);
      })
      .catch((err) => console.log(err));
  }

Here I don't know what to do for the browser to download the file, if anyone know it, let me know.

Here is the nodejs code with express that gives me the error that I mentioned above:

import express from "express";
import { verifyJWT } from "../services/tokens/verifyJWT.js";
import Training from "../models/Training.js";
import fs from "fs";

const path = "/home/marc/Disco/Projects/AtlasFitness/";
const dirname = "uploads/trainings/";

routerTrainer.post("/download-training", verifyJWT, async (req, res) => {
  const { trainer_id, training_id } = req.body;
  let training = await Training.findOne({
    where: { id: training_id, trainer_id },
  });

  if (training) {
    const filePath = fs.createWriteStream(`${path}${dirname}${training.file_id}`);
    res.pipe(filePath);
    filePath.on('finish',() => {
        filePath.close();
        console.log('Download Completed'); 
    })
  }
})

Here is when I get the error, if anyone know how to fix it to get the file downloaded to the user's computer, please let me know it.

Gonsa02
  • 333
  • 3
  • 13
  • You have to return a stream from your node code and in your client code use file-saver library to save it. – Sanish Joseph Aug 29 '21 at 16:02
  • Can you post the code please?? – Gonsa02 Aug 29 '21 at 16:30
  • 1
    These are already answered so I will give a link in so. https://stackoverflow.com/questions/10046039/node-js-send-file-in-response and in react https://www.npmjs.com/package/file-saver – Sanish Joseph Aug 29 '21 at 16:41
  • But when I try to pipe the stream I get the error posted up. "Cannot pipe, not readable" file is a PDF – Gonsa02 Aug 29 '21 at 16:51
  • Is it supposed to be filepath.pipe(res). Filepath being the stream. – Sanish Joseph Aug 29 '21 at 16:59
  • Please, Now I get the following issue https://stackoverflow.com/questions/68975460/how-to-download-a-stream-while-it-is-being-piped-from-backend-nodejs-server-with – Gonsa02 Aug 29 '21 at 17:57
  • Why don't you use use https://stackoverflow.com/questions/10046039/node-js-send-file-in-response. Instead of mp3 you have pdf. You are not setting any header with content type. – Sanish Joseph Aug 29 '21 at 18:03

1 Answers1

2

It should be a read stream on the API, not a write stream.

const filePath = fs.createReadStream(`${path}${dirname}${training.file_id}`);
        res.pipe(filePath);
        filePath.on('finish',() => {
            filePath.close();
            console.log('Download Completed'); 
        })
Janitha Tennakoon
  • 856
  • 11
  • 40
  • When I open the pdf file I get the following error: an error occurred when loading the PDF document – Gonsa02 Sep 04 '21 at 13:38