I'm trying to make a GET request to an express server, and I'm getting the error: "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:3002/hey. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)"
On the frontend
fetch('http://localhost:3002/hey', {
method: "GET",
headers: {
'Access-Control-Allow-Origin': 'http://localhost:3000'
}
})
.then(response => response.text())
.then(data => {
console.log(data);
this.setState({ apiResponse: data })
});
And on the backend
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
app.listen(3002, () => {
console.log('server is running on port 3002');
})
app.get('/hey', (req, res, next) => {
res.send('ho');
})