public
Created

Simple file server test for video

  • Download Gist
mp4-chunk-test.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*jshint node:true*/
var fs = require('fs'),
http = require('http'),
path = require('path'),
port = 1338;
 
http.createServer(function (request, response) {
if (path.extname(request.url) !== '.mp4' || !fs.existsSync('.' + request.url)) return throw404();
 
response.writeHead(200, {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'video/mp4',
'Transfer-Encoding': 'chunked',
'Cache-Control': 'no-store, no-cache, must-revalidate, max-age=0'
});
var file_stream = fs.createReadStream('.' + request.url);
file_stream.on('error', function (exception) {
console.error(exception);
});
file_stream.on('data', function (data) {
response.write(data);
});
file_stream.on('close', function () {
if (request.connection.remoteAddress) console.log('served: ' + request.url + 'to ' + request.connection.remoteAddress);
response.end();
});
 
function throw404() {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end();
return;
}
}).listen(port);
 
console.log('mp4 server ready on http://localhost:' + port);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.