What is Cross-Origin Resource Sharing (CORS) policy?
TL, DR
CORS is a security feature by web browsers that restricts webpages from making requests to resources on a different origin (domain) than the one the web page was served from, unless permission is explicitly granted.
To ignore the security constraints, use something like
With this addition, all functionalities, including direct URL access, fetch in the React project, and direct fetch in the DevTools console, function as expected.
Reason
This is related to the Cross-Origin Resource Sharing (CORS) policy.
In general, browsers are extremely hesitant to talk cross-origin. This is for security reasons.
Think of a user browing evil.com, where the webpage is trying to make transactions on usbank.com. This would be bad! CORS is designed to be a layer of protection against this.
Because http://{server_ip}:53706 is on a different port, these are considered of different origin. Therefore, we can view ``http://{server_ip}:53706/api/hello-world` just fine as the origin is the same. However when doing the same thing cross-origin, our browser does not allow such requests.
When making cross-origin requests, the backend server has to give explicit permission to which webpages can access it cross-origin. In general, we want to limit the "Access-Control-Allow-Origin" to a very small subset of permissible webpages. For this debug example, we can ignore the security considerations to make the code works fine.
Other languages/packages
Nodejs http
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
const http = require('http');
const server = http.createServer((req, res) => { // Set CORS headers res.setHeader("Access-Control-Allow-Origin", req.headers.origin); res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization"); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT, OPTIONS'); res.setHeader('Access-Control-Allow-Credentials', 'true'); res.setHeader('Access-Control-Expose-Headers', 'Set-Cookie');