Skip to content

Instantly share code, notes, and snippets.

@n0m4dz
Forked from padenot/stream.js
Created March 2, 2017 05:07
Show Gist options
  • Save n0m4dz/11f2d9b6334c655bd8ddce881bca8b70 to your computer and use it in GitHub Desktop.
Save n0m4dz/11f2d9b6334c655bd8ddce881bca8b70 to your computer and use it in GitHub Desktop.
How to serve media on node.js
var http = require('http');
var path = require('path');
var fs = require('fs');
var AUDIOFILE = "./audio.ogg";
function serveWithRanges(request, response, content) {
var range = request.headers.range;
var total = content.length;
var parts = range.replace(/bytes=/, "").split("-");
var partialstart = parts[0];
var partialend = parts[1];
var start = parseInt(partialstart, 10);
var end = partialend ? parseInt(partialend, 10) : total;
var chunksize = (end-start);
response.writeHead(206, {
"Content-Range": "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges": "bytes",
"Content-Length": chunksize,
"Content-Type": "audio/ogg"
});
response.end(content.slice(start, end));
}
function serveWithoutContentLength(request, response, content) {
response.writeHead(200, {
"Content-Type": "audio/ogg"
});
response.end(content);
}
function serveWithoutRanges(request, response, content) {
var total = content.length;
var start = 0;
var end = 0;
var chunksize = 0;
start = 0;
end = content.length - 1;
if (request.url.match(".ogg$")) {
response.writeHead(200, {
"Content-Type": "audio/ogg",
"Content-Length": end
});
}
else {
response.writeHead(200, { "Content-Type": "text/html" });
}
response.end(content);
}
function serveHTML(request, response, content) {
response.writeHead(200,
{"Content-Type": "text/html"},
{"Content-Length": content.length}
);
response.end(content);
}
function readcontent(file, callback, request, response) {
var toreturn;
path.exists(file, function(exists) {
if (exists) {
fs.readFile(file, function(error, content) {
if (error) {
response.writeHead(500);
response.end("<h1>500, internal error.</h1>");
toreturn = undefined;
}
else {
callback(request, response, content);
}
});
} else {
response.writeHead(404);
response.end("<h1>404, not found.</h1>");
toreturn = undefined;
}
});
return toreturn;
}
responses = {
// normal response
"/ranges.ogg" : function(request, response) {
content = readcontent(AUDIOFILE, serveWithRanges, request, response);
},
// response without ranges
"/noranges.ogg" : function(request, response) {
readcontent(AUDIOFILE, serveWithoutRanges, request, response);
},
// response without content length
"/nocontentlength.ogg" : function(request, response) {
content = readcontent(AUDIOFILE, serveWithoutContentLength, request, response);
},
// demo page serving
"/index.html" : function(request, response) {
content = readcontent("./index.html", serveHTML, request, response);
},
// empty url
"/" : function(request, response) {
responses["/index.html"](request, response);
}
};
http.createServer(function (request, response) {
var func = responses[request.url];
if (func !== undefined) {
func(request, response);
}
else {
response.writeHead(404);
response.end("404, not found.");
}
}).listen(8125);
console.log('Server running at http://127.0.0.1:8125/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment