-
-
Save Alexzanderk/7bf229993433ac27a5ec62758c47b3a9 to your computer and use it in GitHub Desktop.
Video online
This file contains hidden or 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
app.get('/video', function(req, res) { | |
const path = 'assets/sample.mp4' | |
const stat = fs.statSync(path) | |
const fileSize = stat.size | |
const range = req.headers.range | |
if (range) { | |
const parts = range.replace(/bytes=/, "").split("-") | |
const start = parseInt(parts[0], 10) | |
const end = parts[1] | |
? parseInt(parts[1], 10) | |
: fileSize-1 | |
const chunksize = (end-start)+1 | |
const file = fs.createReadStream(path, {start, end}) | |
const head = { | |
'Content-Range': `bytes ${start}-${end}/${fileSize}`, | |
'Accept-Ranges': 'bytes', | |
'Content-Length': chunksize, | |
'Content-Type': 'video/mp4', | |
} | |
res.writeHead(206, head); | |
file.pipe(res); | |
} else { | |
const head = { | |
'Content-Length': fileSize, | |
'Content-Type': 'video/mp4', | |
} | |
res.writeHead(200, head) | |
fs.createReadStream(path).pipe(res) | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment