I want to send some data in JSON from my React front-end on port 3000 using fetch, to my node.js server on 3005. I configured cors on my server, but every time I try to send request with cookies, Chrome throws error:
Access to fetch at 'http://localhost:3005/user-connected' 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-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.
All console.log's from server code are skipped.
When I delete header in my fetch code
"Content-Type", "application/json"
I get cookies, but without data. With this header included, but without credentials: "include", I can get my data, but I'll never get both at the same time.
Here's my fetch code:
fetch("http://localhost:3005/user-connected", {
mode: "cors",
method: "post",
headers: [
["Content-Type", "application/json"],
],
credentials: "include",
body: JSON.stringify({data: "123"})
})
.then(data => data.json())
.then((data) => {
console.log(`Response: ${JSON.stringify(data)}`);
}).catch(err => {
console.log(`Error: ${err}`)
});
Node.js cors configuration:
app.use(cors({credentials: true}));
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type', 'Access-Control-Allow-Origin', 'Origin');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
And my post route:
let cookiesData = (req, res) => {
console.log(`Cookie user-connected: ${req.cookies.io}`)
console.log(`Received data: ${JSON.stringify(req.body)}`);
res.send({ status: "OK" });
}
router.post("/user-connected", cors({origin: 'http://localhost:3000'}), cookiesData);
Is it even possible to do what I want? Or maybe I missed some important configuration?