Request to server is being blocked despite setting up CORS headers

I am struggling to set up a post route: the issue is that requests are being blocked by CORS rules. I have already set up cors headers both for the preflight request and the post requst.

router.post(
  "/articles",
  withContent,
  async function (request: Request, { SUPABASE_URL, SUPABASE_ANON_KEY }) {
    const corsHeaders = {
      "Access-Control-Allow-Headers": "*",
      "Access-Control-Allow-Methods": "POST",
      "Access-Control-Allow-Origin": "*",
    };
    if (request.method === "OPTIONS") {
      return new Response("OK", {
        headers: corsHeaders,
      });
    }

    if (request.method === "POST") {
      const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
      const { title, content } = request.content;
      try {
        const { data, error } = await supabase
          .from("articles")
          .insert([{ title, content }]);

        if (error) {
          return new Response(JSON.stringify(error), {
            headers: {
              "Content-type": "application/json",
              ...corsHeaders,
            },
          });
        }
        return new Response(JSON.stringify({ success: true, data }), {
          headers: {
            "Content-type": "application/json",
            ...corsHeaders,
          },
        });
      } catch (e) {
        return new Response(JSON.stringify(e), {
          headers: {
            "Content-type": "application/json",
            ...corsHeaders,
          },
        });
      }
    }
  }
);

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.