Express and ytdl-core YouTube Video Info and Download API Not Working as Expected

I’m trying to create an API using Node.js with Express and ytdl-core to fetch YouTube video details and provide a download link. However, the API is not working as expected. Here is my code:

const express = require('express');
const ytdl = require('ytdl-core');
const cors = require('cors');
const app = express();
const port = 5000;

app.use(cors());

app.get('/video-info', async (req, res) => {
  const videoUrl = req.query.url;
  if (!ytdl.validateURL(videoUrl)) {
    return res.status(400).json({ error: 'Invalid YouTube URL' });
  }

  try {
    const info = await ytdl.getInfo(videoUrl);
    const formats = ytdl.filterFormats(info.formats, 'videoonly');
    const videoDetails = {
      title: info.videoDetails.title,
      duration: info.videoDetails.lengthSeconds,
      thumbnail: info.videoDetails.thumbnails[0].url,
      formats: formats.map(format => ({
        itag: format.itag,
        quality: format.qualityLabel,
        container: format.container,
      })),
    };
    res.json(videoDetails);
  } catch (error) {
    res.status(500).json({ error: 'Failed to fetch video details' });
  }
});

app.get('/download', (req, res) => {
  const videoUrl = req.query.url;
  const itag = req.query.itag;

  if (!ytdl.validateURL(videoUrl)) {
    return res.status(400).json({ error: 'Invalid YouTube URL' });
  }

  res.header('Content-Disposition', 'attachment; filename="video.mp4"');

  ytdl(videoUrl, { filter: format => format.itag == itag }).pipe(res);
});

app.listen(port, () => {
  console.log(`Server is running on http://localhost:${port}`);
});

My problems are that the page is just loading and not downloading anything also somehow i made it work in the past but then there were errors with the encoder.