sword1st
sword1st

Reputation: 555

How Can I Feed HTML5 Player With Post Request?

I have a working get request. I took it from stackoverflow. When browser make get request to the express node js server video starts playing. But i want to change it as a post request because i want to choose which video i want. So i want to change players video without refreshing the page. I changed this method to post and i added body-parser to it. Here my method :

app.post('/api/video', urlencodedParser , function(req, res) {
  var folder = req.body.folder
  var path = 'D:/VideoDirectory/'+ folder + '/clip.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)
  }
})

Here my ajax post request :

var folder = {folder: "testFolder"}
$.ajax({
      type: 'POST',
      url: '/api/video',
      data: folder,
      success: function(data){
        alert('post request sent');
      }
    });
}

After i make this post request video is coming to browser. I know that because internet download manager try to catch it. It's have correct file size. But this video doesn't go to the html5 player. How can i feed the player with this post response ? And i want to change video without refresing the page. Here my html5 video player code :

<video id="videoPlayer" controls>
  <source src="http://xxx.xxx.xx.xx:4000/api/video" type="video/mp4">
</video>

Upvotes: 0

Views: 616

Answers (1)

sword1st
sword1st

Reputation: 555

Thanks to @btzr i used get request with parameters. Here it's last form :

    app.get('/api/video/:folder' , function(req, res) {
      var streamer = req.params.folder
      const path = 'D:/VideoDirectory/' + folder+ '/clip.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)
      }
    })

And i'm just changing video player's src with javascript:

var player = document.getElementById('videoPlayer');
player.src = 'http://xxx.xxx.xx.xx:4000/api/video/' + folder;

Video player making get request to the server when src updated.

Upvotes: 1

Related Questions