We force the browser to download 1 MB chunks from the backend

For this MP4 video, we force the browser to download 1 MB chunks. See the Node.js (Express) code below.


app.get('/video', (req, res) => {

    const VIDEO_PATH = path.join(__dirname, './public/videos/997.mp4');
    const CHUNK_SIZE = 1 * 1024 * 1024; // 1 MB


    fs.stat(VIDEO_PATH, (err, stats) => {
        if (err) {
            console.error('File not found:', err);
            return res.sendStatus(404);
        }

        const range = req.headers.range;
        if (!range) {
            res.status(416).set('Content-Range', `bytes */${stats.size}`).end();
            return;
        }

        const parts = range.match(/bytes=(\d+)-(\d*)/);
        if (!parts) {
            return res.status(400).send('Malformed Range header');
        }

        const start = parseInt(parts[1], 10);
        let end = parts[2] ? parseInt(parts[2], 10) : stats.size - 1;

        // Enforce a maximum chunk
        end = Math.min(start + CHUNK_SIZE - 1, end, stats.size - 1);

        const contentLength = end - start + 1;

        res.writeHead(206, {
            'Content-Range': `bytes ${start}-${end}/${stats.size}`,
            'Accept-Ranges': 'bytes',
            'Content-Length': contentLength,
            'Content-Type': 'video/mp4',
        });

        const stream = fs.createReadStream(VIDEO_PATH, { start, end });
        stream.pipe(res);
    });
});

Native behavior, we let the browser decide.