Created
September 18, 2012 22:57
-
-
Save icodeforlove/3746561 to your computer and use it in GitHub Desktop.
Simple file server test for video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*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