Streaming server: TypeError: Cannot call method 'replace' of undefined

Streaming server: TypeError: Cannot call method 'replace' of undefined

我对网络服务器和节点 JS 完全陌生,我一直在关注 this tutorial 在我的 Intel Edison 开发板上创建并理解流媒体服务器。我已经在这个网站上阅读了关于这个问题的其他帖子,但我找不到我的案例的答案

启动 .js 服务器后,我可以在我的客户端浏览器中正确连接到它 [我正在使用 Chrome],例如:http://192.168.1.31:8888/ 和一个视频,与教程,显示。如果我愿意,我什至可以激活它并且它工作正常

但查看提供的代码:

http.createServer(function (req, res) {

    var reqResource = url.parse(req.url).pathname;
    //console.log("Resource: " + reqResource);

    if(reqResource == "/"){

        //console.log(req.headers)
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(indexPage);
        res.end();

    } else if (reqResource == "/favicon.ico"){

        res.writeHead(404);
        res.end();

    } else {

            var total;
            if(reqResource == "/movie.mp4"){
                total = movie_mp4.length;
            } else if(reqResource == "/movie.ogg"){
                total = movie_ogg.length;
            } else if(reqResource == "/movie.webm"){
                total = movie_webm.length;
            } 

            var range = req.headers.range;

            var positions = range.replace(/bytes=/, "").split("-");
            var start = parseInt(positions[0], 10);
            // if last byte position is not present then it is the last byte of the video file.
            var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
            var chunksize = (end-start)+1;

            if(reqResource == "/movie.mp4"){
                res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total, 
                                     "Accept-Ranges": "bytes",
                                     "Content-Length": chunksize,
                                     "Content-Type":"video/mp4"});
                res.end(movie_mp4.slice(start, end+1), "binary");

            } else if(reqResource == "/movie.ogg"){
                res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total, 
                                     "Accept-Ranges": "bytes",
                                     "Content-Length": chunksize,
                                     "Content-Type":"video/ogg"});
                res.end(movie_ogg.slice(start, end+1), "binary");

            } else if(reqResource == "/movie.webm"){
                res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total, 
                                     "Accept-Ranges": "bytes",
                                     "Content-Length": chunksize,
                                     "Content-Type":"video/webm"});
                res.end(movie_webm.slice(start, end+1), "binary");
            }
    }
}).listen(8888);

我想我也应该能够在我的客户中输入如下内容: http://192.168.1.31:8888/movie.mp4 并解决此地址在我的 chrome 客户端上正确接收视频片段的问题。问题是,当我尝试这样做时,出现以下错误:

 var positions = range.replace(/bytes=/, "").split("-");
                                  ^
TypeError: Cannot call method 'replace' of undefined
    at Server.<anonymous> (/home/root/streaming-video-html5/streaming-video-html5/server.js:66:35)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2109:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
    at Socket.socket.ondata (http.js:1967:22)

我从 here 了解到这个错误是由于 range.replace.split undefined 但为什么呢?

知道如何解决这个烦人的问题吗?

在此先感谢那些愿意提供帮助的人

客户端可能不会在请求中发送 range header - 这不是强制性的,所以试试这个

    var range = req.headers.range;
    var start = 0, end = total - 1;
    if(range) {
        var positions = range.replace(/bytes=/, "").split("-");
        start = parseInt(positions[0], 10);
        // if last byte position is not present then it is the last byte of the video file.
        end = positions[1] ? parseInt(positions[1], 10) : total - 1;
    }

我认为应该可以解决这个问题