So I'm trying to make an axios GET request to my Node.js/Express.js localhost:3000 server from my front-end app on localhost:8080 and I understand why I am getting a CORS error. What I don't understand is why after I installed the cors npm package and used it as middleware on my Node.js/Express.js back-end, I still get the CORS error.
I did this inside the app.js file
var cors = require('cors');
app.use(cors());
Then I still get the following error when I try to make a GET request from my front-end:
Access to XMLHttpRequest at 'http://localhost:3000/images' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Shouldn't me using the cors package solve this problem?
You have to allow cors to receive that origin. There's a good good video about this. But adding the parameter origin to the cors() middleware should solve it.
var cors = require('cors');
app.use(cors({
origin: "http://localhost:8000"
}));