-
-
Save awsp/7a375824865703cf278d0319b8bdfb1d to your computer and use it in GitHub Desktop.
streaming mp3 using nodejs
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 http = require('http'), | |
fileSystem = require('fs'), | |
path = require('path') | |
util = require('util'); | |
http.createServer(function(request, response) { | |
var filePath = 'path_to_file.mp3'; | |
var stat = fileSystem.statSync(filePath); | |
response.writeHead(200, { | |
'Content-Type': 'audio/mpeg', | |
'Content-Length': stat.size | |
}); | |
var readStream = fileSystem.createReadStream(filePath); | |
// We replaced all the event handlers with a simple call to util.pump() | |
util.pump(readStream, response); | |
}) | |
.listen(2000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment