Angular and coros

Title: CORS Policy Issue: Blocked XMLHttpRequest Access from localhost:4200 to localhost:5000


Problem:
I’m encountering an issue where I keep getting bombarded with the following error:

Access to XMLHttpRequest at 'http://localhost:5000/compare-with-csv' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. :5000/compare-with-csv:1
Failed to load resource: net::ERR_FAILED
core.mjs:7494 ERROR HttpErrorResponse

Context:
I’m currently working on a project where I have a frontend running on localhost:4200 and a backend on localhost:5000. The frontend is attempting to make a request to the backend, specifically to the endpoint compare-with-csv.

What I’ve Tried:
I’ve already implemented CORS handling on my backend, yet the error persists. I’ve ensured that the CORS middleware is correctly configured to allow requests from localhost:4200. Despite this, the error continues to occur, causing the XMLHttpRequest to fail.

Code Snippets:
Here’s a snippet of my CORS middleware configuration on the backend:

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import Chart from 'chart.js/auto';


@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
  //declaring
// Overview Data
  
  // Demographics Section
 

  // Academic Performance Section
  


  // Intervention Insights Section
 
  constructor(private http: HttpClient) { }

  ngOnInit(): void {
    this.fetchData();
  }

  fetchData(): void {
    this.http.get<any>('http://localhost:5000/compare-with-csv')
      .subscribe((data: any) => {
        // Overview Data


// Demographics Section

//assing data 
// Academic Performance Section


// Intervention Insights Section


        // Intervention Insights Section
        // Assuming studentsAtRiskData, studentsAtRiskLabels, interventionTrendsData, 
      });

    // Assigning values fetched from backend to variables
consolloging data

// Overview Data

// Demographics Section

// Academic Performance Section

// Intervention Insights Section

  }
}

Question:
What else could be causing this CORS policy error even after ensuring that CORS is correctly implemented on my backend? Is there any additional configuration or troubleshooting step I might be missing?

Any insights or guidance would be greatly appreciated! Thank you in advance.
send me dm if you want the full code

I attempted to resolve the CORS policy error by implementing CORS middleware in the backend code. Specifically, I configured the CORS middleware to allow requests from localhost:4200, which is where my frontend is hosted. Additionally, I ensured that the CORS middleware is correctly configured to allow the appropriate HTTP methods and headers.

My expectation was that by implementing CORS correctly, the error message indicating that the XMLHttpRequest is blocked due to CORS policy would no longer occur. I expected the frontend request to the backend endpoint compare-with-csv to be successfully processed without encountering any CORS-related issues. However, despite implementing CORS, the error persisted, which led me to seek further assistance in troubleshooting the issue.