2015-08-05 7 views
5

A volte ricevo un errore sul mio server Node.js che mi dice che req.headers.range non è definito quando un utente richiede uno .mp4 (non ho provato a servire altri tipi di file video, non sono sicuro che mp4 sia rilevante). Non capisco perché manca questa intestazione.Intestazione intervallo mancante dalla richiesta video?

Se un'intestazione di richiesta per un video a volte esclude range?

ho inviare il flusso in questo modo:

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); 
      }); 
     } 
    }); 
}; 

L'errore:

/video_stream.js:21 
     var positions = range.replace(/bytes=/, "").split("-"); 
     TypeError: Cannot call method 'replace' of undefined 

risposta

2

L'intestazione Range è facoltativo:

HTTP retrieval requests using conditional or unconditional GET methods MAY request one or more sub-ranges of the entity, instead of the entire entity, using the Range request header (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2)

Non importa se la richiesta è per un flusso video o no. Suppongo che un browser (o una libreria di lettori video) non invii un'intestazione Range per la richiesta iniziale e la utilizzi solo durante la ricerca.

1

Dovresti assicurarti di rispondere a una richiesta GET. Potrebbe essere che il browser invii una richiesta HEAD per ottenere le informazioni sul file. Le richieste HEAD non contengono intestazioni di intervallo.