Skip to content

Instantly share code, notes, and snippets.

@ArunMichaelDsouza
Forked from dtrce/mp3.js
Last active August 29, 2015 14:27
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 ArunMichaelDsouza/59e10ba5f2ba63c9578f to your computer and use it in GitHub Desktop.
Save ArunMichaelDsouza/59e10ba5f2ba63c9578f 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