How to send a file from remote URL as a GET respon

2019-01-22 14:17发布

问题:

Context: Multi-tier Node.js app with Express. Front end hosted as Azure website, back end data is coming from Parse.

I have a GET endpoint and I want the user experience to be a file download. If the file lived in the same server as my front end, I could use

res.sendfile(localPath);

However, the file is hosted by my back end on another server and all my front end knows is the URL. So I'm currently doing:

res.redirect(externalURL);

This results in the browser navigating to the asset file URL (which is a video) and it displays in the browser. What I want to know is: how to have the browser download that file instead and just stay at the same page?

I've searched around for a while on this and I don't see a way of doing it without first downloading the file to a temporary file on my front end, which I'd obviously rather not do. This is probably HTTP 101 stuff so I appreciate any help!

回答1:

You can use the http.request() function to make a request to your externalURL and then pipe() the response back to res. Using pipe() will stream the file through your server to the browser, but it won't save it to disk at any point. If you want the file to be downloaded as well (as opposed to just being displayed in the browser), you'll have to set the content-disposition header.

Here's an example of a server which will "download" the google logo. This is just using the standard http module and not express, but it should work basically the same way:

var http = require('http');

http.createServer(function(req, res) {
    var externalReq = http.request({
        hostname: "www.google.com",
        path: "/images/srpr/logo11w.png"
    }, function(externalRes) {
        res.setHeader("content-disposition", "attachment; filename=logo.png");
        externalRes.pipe(res);
    });
    externalReq.end();
}).listen(8080);

If you want to use the request module, it's even easier:

var http = require('http'),
    request = require('request');

http.createServer(function(req, res) {
    res.setHeader("content-disposition", "attachment; filename=logo.png");
    request('http://google.com/images/srpr/logo11w.png').pipe(res);
}).listen(8080);

Note: The name of the file that the browser will save is set by the filename= part of the content-disposition header; in this case I set it to logo.png.