0

I would like to stream different user-selected videos to my front-end. For this I am using NodeJS and Express. The source of the -element in which the video should be displayed is 'http://localhost:4201/video'.

The code I am using to stream the video looks like this:

async function loadLocalVideo(_, filePath) {
    if (!filePath) {
        console.log('No file selected');
        return;
    } else {
        fs.access(filePath, (err) => {
            if (err) {
                console.log(`File does not exist at path ${filePath}`);
                return;
            }
        });
    }

    expressApp.get('/video', function (req, res) {
        const path = filePath;
        const stat = fs.statSync(path);
        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;

            if (start >= fileSize) {
                res.status(416).send(
                    'Requested range not satisfiable\n' + start + ' >= ' + fileSize
                );
                return;
            }

            const chunksize = end - start + 1;
            const file = fs.createReadStream(path, { start, end });
            const head = {
                'Content-Range': `bytes ${start}-${end}/${fileSize}`,
                'Accept-Ranges': '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);
        }
    });
}

However, when I want to stream a different video and call the same function again but with a different filePath-param the same video keeps playing. How can I stream another video and display it in the -element?

GyroGoober
  • 61
  • 8

1 Answers1

0

I think you are saying that you are using the same file name and path but want the backend server to change the actual contents that are streamed to the client.

This approach may confuse the caching downstream of your server - i.e. the network or browser cache may not recognise that the file has changed and it may simply serve the cached versions.

Some CDN's will allow you add edge functionality to intercept a request and then decide which of a number of options you want to return, or you could disable caching but I suspect these approaches might be overly complex.

If all you want is to be able to display different videos in a video element on your web page, it may be easier to simply change the source attribute on the video on the page itself and then call load() on the video element on the page.

Mick
  • 24,231
  • 1
  • 54
  • 120
  • Thank you very much! Changing the URL did the trick for me. I decided to append a unique video-id to the end of the URL so all of them are unique. – GyroGoober Apr 21 '22 at 18:13