Skip to content

Instantly share code, notes, and snippets.

@jessecravens
Created November 26, 2012 18:54
Show Gist options
  • Save jessecravens/4149914 to your computer and use it in GitHub Desktop.
Save jessecravens/4149914 to your computer and use it in GitHub Desktop.
NodeJS Hacks - Streams - Mp3
exports.example2 = function(req, res){
var http = require('http'),
fs = require('fs'),
util = require('util');
var mp3File = __dirname + "/../TRB_9.21.02_Manatee.mp3";
var stat = fs.statSync(mp3File); // we need some info about the file size
res.writeHead(200, {
'Content-Type': 'audio/mpeg',
'Content-Length': stat.size
});
var readStream = fs.createReadStream(mp3File);
// instead of events we'll make a single call to a helper function
util.pump(readStream, res);
//res.render('example2', { title: 'Example2' })
};
@jessecravens
Copy link
Author

Request URL:http://localhost:3000/example2
Request Method:GET
Status Code:200 OK

Request Headers
Accept:/
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:identity;q=1, *;q=0
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:false_r=1; __atuvc=85%7C38%2C4%7C39%2C1%7C40
Host:localhost:3000
Range:bytes=0-
Referer:http://localhost:3000/example2
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11

Response Headers
Connection:keep-alive
Content-Length:5048320
Content-Type:audio/mpeg
Date:Mon, 26 Nov 2012 18:55:32 GMT
X-Powered-By:Express

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