Skip to content

Instantly share code, notes, and snippets.

@bnerd
Created March 10, 2012 11:54
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bnerd/2011232 to your computer and use it in GitHub Desktop.
Save bnerd/2011232 to your computer and use it in GitHub Desktop.
Serve large files with Node.js
var libpath = require('path');
var http = require('http');
var fs = require('fs');
var url = require('url');
var bind_port = 8001;
var path = "/path/to/your/base_directory/";
http.createServer(function (request, response) {
var uri = url.parse(request.url).pathname;
var filename = libpath.join(path, uri);
libpath.exists(filename, function (exists) {
if (!exists) {
console.log('404 File Not Found: ' + filename);
response.writeHead(404, {
"Content-Type": "text/plain"
});
response.write("404 Not Found\n");
response.end();
return;
} else{
console.log('Starting download: ' + filename);
var stream = fs.createReadStream(filename, { bufferSize: 64 * 1024 });
stream.pipe(response);
}
});
}).listen(bind_port);
console.log('Download Server listening on Port' + bind_port);
@ripper2hl
Copy link

what is bufferSize ?

@dreaddymck
Copy link

I think

{ bufferSize: 64 * 1024 } 

is now

{ highWaterMark: 64 * 1024 }

@odilitime
Copy link

libpath.exists probably should be fs.exists

@jiangying000
Copy link

libpath.exists probably should be fs.exists

yes

@d0n0t
Copy link

d0n0t commented Sep 21, 2020

can we add a m3u8 live stream to heroku? i am failing everytime,i guess i dont have correct buildpack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment