Skip to content

Instantly share code, notes, and snippets.

@prayagupa
Forked from dtrce/mp3.js
Created October 17, 2016 03:59
Show Gist options
  • Save prayagupa/e0f74060b8315523281d7edea8651df4 to your computer and use it in GitHub Desktop.
Save prayagupa/e0f74060b8315523281d7edea8651df4 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment