Ошибка ERR_HTTP_HEADERS_SENT - для потокового видео POV - PullRequest
0 голосов
/ 30 марта 2020

Я работаю над приложением потокового видео, использующим nodejs с e js в качестве движка шаблонов, и каждый раз, когда я использую этот код, он выдает ошибку HTTP-заголовков, присылающих любые советы или решения, которые приветствуются. спасибо.

app.get("/watch/:watches",(req,res)=>{
const requestedPostId = req.params.watches;
    Watch.findOne({_id: requestedPostId}, function(err, watch){ //the parameters for this is the err and the ejs file which it need's to send
    const path='videos/'+watch.src;
    fs.stat(path,(err,stat)=>{
        console.log(path);
        if (err!=null && err.code==='ENOENT') {
            res.sendStatus(404);
        }
        const filesize=stat.size
        const range=req.headers.range
        if (range) {
            const parts=range.replace(/bytes=/,"").split("-");
            const start=parseInt(parts[0],10);
            const end=parts[1]?parseInt(parts[1],10):filesize-1;

            const chunksize=(end-start)+1;
            const file=fs.createReadStream(path,{start,end});
            const head={
                'Content-Range' : 'bytes ${start}-${end}/${filesize}',
                'Accept-Range' : 'bytes',
                'Content-Length' : chunksize,
                'Content-Type' : 'video/mp4'
            }
            res.writeHead(206,head);
            file.pipe(res);
        }else{
            const head={
                'Content-Length':filesize,
                'Content-Type':'video/mp4'
            }
            res.writeHead(200,head);
            fs.createReadStream(path).pipe(res);
        }
    });
    res.render("watch", {
            // source:watch.src,
            title : watch.title,
            content : watch.content
            });
        });

});
...