Skip to content

Instantly share code, notes, and snippets.

@raminious
Created October 6, 2014 20:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raminious/6a53c88d8ffb64744751 to your computer and use it in GitHub Desktop.
Save raminious/6a53c88d8ffb64744751 to your computer and use it in GitHub Desktop.
my node streamer
var http = require('http');
var path = require('path');
var fs = require('fs');
var stream = require('stream');
http.createServer(function (req, res) {
//res.writeHead(200, {'Content-Type': 'text/plain'});
var filename = [__dirname, 'file.mp3'].join(path.sep);
fs.stat(filename, function(error, stats){
stream = fs.createReadStream(filename);
stream.on('data', function(data) {
if (res.write(data) == false){
stream.pause();
}
});
stream.on('end', function(){
res.end();
});
res.on('drain', function(){
stream.resume();
});
res.on('close', function(){
stream.close();
});
});
})
.listen(1337, '127.0.0.1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment