javascript - Why do the CORS settings for my Express.js backend not work? - Stack Overflow

PHOTO EMBED

Mon Oct 02 2023 13:14:07 GMT+0000 (Coordinated Universal Time)

Saved by @daavib #javascript

var whiteList = {
    "http://localhost:5000": true,
    "https://example-url.herokuapp.com": true
};
var allowCrossDomain = function(req, res, next) {    
        if(whiteList[req.headers.origin]){            
            res.header('Access-Control-Allow-Credentials', true);
            res.header('Access-Control-Allow-Origin', req.headers.origin);
            res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
            res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, Origin, Accept');        
            next();
        } 
};
app.use(allowCrossDomain);
content_copyCOPY

https://stackoverflow.com/questions/29196364/why-do-the-cors-settings-for-my-express-js-backend-not-work?rq