V
V
Vladimir Sergeevich2016-04-29 00:56:54
Video
Vladimir Sergeevich, 2016-04-29 00:56:54

Get request to node.js server sending Range headers?

Peace for everyone. And thanks in advance!
Task:
Receive video content via a Get request to another server, pass traffic through itself and stream to the client on its own behalf. I must say right away that we stream videos from VK.
Node.js was chosen because of its ability to work with threads flexibly, I don’t load RAM, at the start a simple example from the description of the request plugin was used for this task

http.createServer(function (req, resp) {
  if (req.url === '/doodle.png') {
    var x = request('http://mysite.com/doodle.png')
    req.pipe(x)
    x.pipe(resp)
  }
})

And everything is fine, the stream is going on, the video is rewinding ... But 2 big problems:
1) the buffer is not cleared
2) There is no destroy method for the request stream, but it is required (In the case of a long video)
After looking at what they write on GitHub, the problem cannot be solved succeeded and had to jump to a similar scheme:
app.get('/video', function (req, res) {

  <!-- ===[Обязательные заголовки]=== -->
  res.statusCode = 206;
  res.setHeader('Content-type', 'video/mp4');
  res.setHeadeq('Access-Control-Allow-Origin', '*');

  // ===[Обработка запроса]=== -->
  var file_url = url.parse(req.url, true).query.file; // Чистая ссылка на файл

  if (req.headers.range) {
    var request_range = req.headers.range;
    var parts = request_range.replace(/bytes=/, "").split("-");
    var partialstart = parts[0];
    var partialend = parts[1];
  }

  console.log(req.headers.range);

  // var x = request(file_url, function (err, response, body) {
  //     console.log("ФАЙЛ ПРОЧИТАН!");
  // }).pipe(res);

  var request = https.get(file_url, function (response) {

    // ===[Обработка ответа]=== -->
    var content_length = response.headers["content-length"];
    var start = parseInt(partialstart, 10);
    var end = partialend ? parseInt(partialend, 10) : content_length - 1;
    var chunksize = (end - start) + 1;
    console.log('RANGE: ' + start + ' - ' + end + ' = ' + chunksize);

    res.setHeader('Content-Length', content_length);
    res.setHeader('Accept-Ranges', 'bytes');
    res.setHeader('Content-Range', 'bytes ' + start + '-' + end + '/' + content_length);

    response.pipe(res);

  });

  // setInterval(function () {
  //   console.log(process.memoryUsage().rss / 1024 / 1024);
  // }, 3000)

});

And of course, video rewind does not work here, but the stream is not clogged into memory.
Essence of the question:
Can anyone give advice in which direction to look, or advise how you can make a Get request with headers to get a file from a specific location.
This is my first experience with NodeJs, the question may seem silly, but I still don't fully know all the methods and basics. Thanks again!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-04-29
@VladimirZhid

If you still want to do it yourself, then the specification will help: https://tools.ietf.org/html/rfc7233
In general, we configure nginx-proxy and do not bother

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question