i'm using an external api, i setup express.js as my backend and have app.use(cors()) to get around cors policy error but im still getting the error when im trying to make an api call in react
server setup
server.js
const express = require('express')
const app = express();
const cors = require('cors')
app.use(cors({
origin: '*'
}));
const port = 5000;
app.listen(port, () => console.log(`Server started on port ${port}`));
API call
app.js
useEffect(() => {
async function datareciever() {
const response = await fetch("api_url")
const data = await response.json()
console.log(data)
}
datareciever()
}, [])
error that I'm receiving in the browser
Access to fetch at 'api_url' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I've also tried setting the port to 3000, but the express server takes up port 3000 so react then switches to 3001.
I believe origin: '*' should allow for any port/domain, so I don't think that could be the issue.
Current file structure(if it matters). React is inside expressapp folder.
What could be causing the issue?