Answer the question
In order to leave comments, you need to log in
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)
}
})
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)
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question