Created
May 22, 2020 07:58
-
-
Save vitorvargasdev/8ff35976000c6f8ac4273ce0cff35ae0 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
<template> | |
<div id="app"> | |
<video width="400" controls> | |
<source src="http://localhost:8888" type="video/mp4" /> | |
</video> | |
</div> | |
</template> | |
<script> | |
export default { | |
data: () => ({ | |
video: '' | |
}), | |
methods: { | |
openVideo (folder, video, port) { | |
var fs = require('fs') | |
var http = require('http') | |
var path = require('path') | |
http | |
.createServer(function (req, res) { | |
var file = path.resolve(folder, video) | |
fs.stat(file, function (err, stats) { | |
if (err) { | |
if (err.code === 'ENOENT') { | |
// 404 Error if file not found | |
return res.sendStatus(404) | |
} | |
res.end(err) | |
} | |
var range = req.headers.range | |
if (!range) { | |
// 416 Wrong range | |
return res.sendStatus(416) | |
} | |
var positions = range.replace(/bytes=/, '').split('-') | |
var start = parseInt(positions[0], 10) | |
var total = stats.size | |
var end = positions[1] ? parseInt(positions[1], 10) : total - 1 | |
var chunksize = end - start + 1 | |
res.writeHead(206, { | |
'Content-Range': 'bytes ' + start + '-' + end + '/' + total, | |
'Accept-Ranges': 'bytes', | |
'Content-Length': chunksize, | |
'Content-Type': 'video/mp4' | |
}) | |
var stream = fs | |
.createReadStream(file, { start: start, end: end }) | |
.on('open', function () { | |
stream.pipe(res) | |
}) | |
.on('error', function (err) { | |
res.end(err) | |
}) | |
}) | |
}) | |
.listen(port) | |
} | |
}, | |
beforeMount () { | |
this.openVideo('/home/instalando', '1.mp4', 8888) | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment