John Keel
John Keel

Reputation: 99

how to stream a file to server and synchronously stream it to user? (usual file download imitation)

I need to make a downloadable link for a file, but, for certain reasons I cannot load file directly. Is there any way to stream a file from its destination to remote server and immediately (at the same time) from server to user. Thank you.

Upvotes: 1

Views: 840

Answers (1)

mscdex
mscdex

Reputation: 106696

You can use fs.createReadStream() and pipe that to the response object. For example:

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

http.createServer(function(req, res) {
  res.writeHead(200, { 'Content-Type': 'text/javascript' });
  fs.createReadStream(__filename).pipe(res);
}).listen(8000);

Upvotes: 1

Related Questions