Created
January 28, 2016 23:45
-
-
Save linfongi/0b5bc0193af191b71a69 to your computer and use it in GitHub Desktop.
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
var http = require('http'); | |
var fs = require('fs') | |
var util = require('util'); | |
http.createServer(function(req, res) { | |
var path = './test.mp4'; | |
var stat = fs.statSync(path); | |
var total = stat.size; | |
if (req.headers.range) { | |
var range = req.headers.range; | |
var parts = range.replace(/bytes=/, "").split("-"); | |
var partialstart = parts[0]; | |
var partialend = parts[1]; | |
var start = parseInt(partialstart); | |
var end = partialend ? parseInt(partialend) : total - 1; | |
var chunksize = (end - start) + 1; | |
console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize); | |
var file = fs.createReadStream(path, { | |
start: start, | |
end: end | |
}); | |
// log buffer data | |
// file.on('data', function(buffer) { | |
// console.log('READ BUFFER', buffer.length); | |
// }); | |
res.on('drain', function(buffer) { | |
console.log('Sending'); | |
}); | |
res.writeHead(206, { | |
'Content-Range': 'bytes ' + start + '-' + end + '/' + total, | |
'Accept-Ranges': 'bytes', | |
'Content-Length': chunksize, | |
'Content-Type': 'video/mp4' | |
}); | |
file.pipe(res); | |
} else { | |
console.log('ALL: ' + total); | |
res.writeHead(200, { | |
'Content-Length': total, | |
'Content-Type': 'video/mp4' | |
}); | |
fs.createReadStream(path).pipe(res); | |
} | |
}).listen(8866, '127.0.0.1'); | |
console.log('Server running at http://127.0.0.1:8866/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment