Created
March 10, 2012 11:54
-
-
Save bnerd/2011232 to your computer and use it in GitHub Desktop.
Serve large files with Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
libpath.exists probably should be fs.exists
libpath.exists probably should be fs.exists
yes
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
I think
is now