Skip to content

Instantly share code, notes, and snippets.

@dtrce
Created September 8, 2011 18:39
Show Gist options
  • Save dtrce/1204243 to your computer and use it in GitHub Desktop.
Save dtrce/1204243 to your computer and use it in GitHub Desktop.
streaming mp3 using nodejs
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);
@dirkk0
Copy link

dirkk0 commented Jul 12, 2022

^^^ works like charm, thanks!

@leruetkins
Copy link

How to make mp3 stream from folder?

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