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

174
Vistas
I can't download a file with nodejs backend and react frontend

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.

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

0

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'); 
        })
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