Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!