I sometimes get an error on my Node.js server telling me req.headers.range
is undefined when a user is requesting a .mp4
(I haven't tried serving any other video filetypes, not sure if mp4 is relevant). I don't understand why this header is missing.
Should a request header for a video sometimes exclude range
?
I send the stream like this:
var videoReqHandler = function(req, res, pathname) {
var file = dirPath + "/Client" + pathname;
var range = req.headers.range;
var positions = range.replace(/bytes=/, "").split("-");
var start = parseInt(positions[0], 10);
fs.stat(file, function(err, stats) {
if (err) {
throw err;
} else {
var total = stats.size;
var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
var chunksize = (end - start) + 1;
res.writeHead(206, {
"Content-Range" : "bytes " + start + "-" + end + "/" + total,
"Accept-Ranges" : "bytes",
"Content-Length" : chunksize,
"Content-Type" : "video/mp4"
});
var stream = fs.createReadStream(file, {
start : start,
end : end
}).on("open", function() {
stream.pipe(res);
}).on("error", function(err) {
res.end(err);
});
}
});
};
The error:
/video_stream.js:21
var positions = range.replace(/bytes=/, "").split("-");
TypeError: Cannot call method 'replace' of undefined