Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http'), fileSystem = require('fs'), path = require('path'), util = require('util');
- http.createServer(function(request, response){
- var filePath = path.join(__dirname, "stream", "video.mp4");
- var stat = fileSystem.statSync(filePath);
- // inicio do pedaço
- var inicio = 0;
- // Fim do pedaço
- var fim = 0;
- var range = request.headers.range;
- console.log(range);
- if (range) {
- console.log('Existe range');
- inicio = parseInt(range.slice(range.indexOf("byte=")+6,range.indexOf("-")));
- fim = parseInt(range.slice(range.indexOf("-")+1, range.length));
- }
- if (inicio > fim)
- return;
- if (isNaN(fim) || fim == 0) {
- fim = stat.size - 1;
- }
- response.write(206, {
- 'Content-Type' : 'video/mp4',
- 'Content-Range' : 'bytes ' + inicio + '-' + fim + '/' + stat.size,
- 'Content-Length': stat.size,
- 'Transfer-Encoding':'chunked'
- });
- console.log(inicio);
- console.log(fim);
- var readStream = fileSystem.createReadStream(filePath, { flags: 'r', start: inicio, end: fim });
- util.pump(readStream, response);
- }).listen(3000);
Add Comment
Please, Sign In to add comment