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

213
Vistas
HTTP Get file from different site and return with Express for download

I'm trying to download a file from another site from my Node app after an express HTTP get request and then return the file for download. I've tried multiple ways of getting the file, using pipe, blob, etc. but I'm grasping in the dark. The code might give you a bit more of an insight as to what I'm trying to achieve:

var router = require('express').Router();
var fs = require('fs');
var http = require('http');

router.get('/download/:file', function (req, res, next) {
  http.get('http://anothersite/' + req.params.file, function(response) {
    res.setHeader('Content-disposition', 'attachment; filename=' + req.params.file);
    res.setHeader('Content-type', 'application/octet-stream');
    res.download(fs.createWriteStream(req.params.file).pipe(response));
  });
});

This gives me an error "Cannot pipe. Not Readable". The file itself is not a regular file format (it's a file from our customized software with its own extension).

What am I missing?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

For one you need to use readable stream here, not writable

The Express res object is derived from node's http.ServerResponse, which is itself implementing node's WritableStream interface. See docs here and here.

Since that is the case, I think you can use response argument passed to your callback directly, since that is already a ReadableStream (see here). Try using readable stream like this:

router.get('/download/:file', function (req, res, next) {
  http.get('http://anothersite/' + req.params.file, function(response) {
    res.setHeader('Content-disposition', 'attachment; filename=' + req.params.file);
    res.setHeader('Content-type', 'application/octet-stream');
    response.pipe(res);  //  <-- change here
  });
});

This code is working, with node v5.0.0 and latest chrome:

const express = require('express');
const app = express();
const http = require('http');

app.get('/', (req, res) => {
  http.get('http://www.fillmurray.com/200/300', (response) => {
    res.setHeader('Content-disposition', 'attachment; filename=' + 'hello.jpg');
    res.setHeader('Content-type', 'application/octet-stream');
    response.pipe(res)
  });
});

app.listen(3001, () => console.log(('listening :)')))
about 4 years ago · Santiago Trujillo Denunciar

0

you can use request library as: request('http://anothersite/').pipe(fs.createWriteStream('filename.extension'))

Update:

If you are willing to do it by http you can create a write stream to as follow. After saving the file successfully I think you can do res.download() correctly.

var http = require('http')

http.get('http://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png').on('response', function (response) {
    var writeStream = fs.createWriteStream('output.png');

    response.pipe(writeStream);

    response.on('end', function () {
        console.log('stream completed!')
    });

    // This is here incase any errors occur
    writeStream.on('error', function (err) {
        console.log(err);
    });

});
about 4 years ago · Santiago Trujillo 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