Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda