I want to stream video in node.js through fs module & I have tried the below code.
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)
  }
})
My video 'sample.mp4' size is: 1055736 bytes which is 1 MB. But when I run the code, I found that in starting the start variable is 0, end  variable is 1055735 & chunksize is 1055736. And if you look at Content-Range header, which will be equals to below value in starting.
Content-Range : 'bytes 0-1055735/1055736'
I want to stream my video in chunks one after another instead of wait for the whole video to load. But I think above code will not do the same.
Can anyone please tell me, Is this code correct for video streaming?
Thanks in advance!