/*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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment