'why i can't connect with my api , the header of requset is (blocked by cors) nodejs

I want to return data from my graphql API, I have two front-end in one of these is work, but in another front-end, it doesn't work

when I run the first front end in this port it works but for the second one to doesn't work and it says blocked by cors policy

hare is my back end code

import cors from "cors"; app.use( cors({ origin: "http://localhost:3000", credentials: true, }) );

also, I try but is doesn't work

app.use(cors())



Solution 1:[1]

You need to add the second frontend as allowed origin, for example if the second fronted is served from http://localhost:3001 do this:

cors({ 
  origin: ["http://localhost:3000", "http://localhost:3001"], 
  credentials: true
})

If you want to allow all origins you can set origin: true.

Look here for all the allowed options for origin:
https://www.npmjs.com/package/cors#configuration-options

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1