Setheron
Setheron

Reputation: 3740

NodeJS: Is there a way to stream a file from another server without piping through mine?

I have a site in which I provide links for download/streaming for media files on another server. Currently how I have it setup is that something similar to the following:

download = (req, res) ->
  song_id = req.params.id
  download_url = "http://mediaserver.com/#{song_id}"
  console.log("Returning download from: #{download_url}")
  res.set('Content-Type', 'audio/mpeg')
  res.set('Content-Disposition', 'attachment; filename="download.mp3"')
  request.get(download_url).pipe(res)
  #For some reason, express.js on a response end event emits a finish
  #http://www.samcday.com.au/blog/2011/06/27/listening-for-end-of-response-with-nodeexpress-js/
  res.on "finish", ()->
    console.log("Completed song download")

However, I'm looking for a way I can just return a HTTP response object that tells the browser to just download it from the appropriate server instead of my own? (possibly still changing the HTTP headers to allow the file to be saved as a download).

Any way this is possible?

TY

Upvotes: 1

Views: 1315

Answers (2)

groodt
groodt

Reputation: 1993

Can you not simply provide the links to the server hosting the actual files, then the browser will download them when clicked?

e.g.

<a href="http://www.assets.com/file.mp3">File</a>

Upvotes: 0

Brad
Brad

Reputation: 163282

Simply use a 301 (permanent) or 302 (temporary) redirect with a location header.

response.writeHead(302, {
  'Location': 'http://someUrlHere'
});

I'm not an Express user, but it looks like you can set the status code in Express with res.send(302).

Upvotes: 2

Related Questions