Access control allow origin is undefined even though I am setting the header

18 views Asked by At

My client is running on the port 3000 and my apollo server is running on port 4000. I tried this to get rid of CORS issue:

app.use(
"/graphql",
cors({
    origin: "http://localhost:3000",
    credentials: true,
}),
express.json(),
expressMiddleware(server, {
    context: async ({ req, res }) => buildContext({ req, res }),
})

But still getting the error Access to fetch at 'http://localhost:4000/graphql' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.

I also tried to set the header manually like this:

app.use("/graphql", (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "http://localhost:3000");
res.setHeader("Access-Control-Allow-Credentials", "true");
next(); // Call next middleware

});

But still getting the same error. What might be the issue?

0

There are 0 answers