I would like to make a download on client-side for a Google CDN file.
I have some links on my page that when clicked should generate a download. Something like:
<a href="//ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js">Link</a>
would be sent as a download to the client.
I tried like:
app.get('/download/:version?', function(req, res){
fs.readFile('http://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js', function (err, data) {
if (err) throw err;
res.download(data);
});
});
But that didn't do it. Is this the right approach?
fs.readFile() only supports paths on the local system.
Try using http.request()/http.get() instead:
var http = require('http');
// ...
app.get('/download/:version?', function(req, res){
// set Content-Disposition header
res.attachment('prototype.js');
// transfer the file
http.get('http://ajax.googleapis.com/ajax/libs/prototype/1.7.2.0/prototype.js')
.pipe(res);
});