Monday, October 17, 2022

Cross-origin resource sharing (CORS) with Express Node.js

https://www.stackhawk.com/blog/react-cors-guide-what-it-is-and-how-to-enable-it/

////////////////////////////////////////////////////////
// Cross-origin resource sharing (CORS)
////////////////////////////////////////////////////////
const cors = require('cors');
app.use(cors());

app.get(
'url',
function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept'
);
res.json({ msg: 'This is CORS-enabled for all origins!' });
next();
}
);

app.listen(8080, '127.0.1.1', function () {
console.log('CORS-enabled web server listening on port 8080');
});

https://stackoverflow.com/questions/43262121/trying-to-use-fetch-and-pass-in-mode-no-cors

https://stackoverflow.com/questions/35068712/error-listen-eacces-0-0-0-080-osx-node-js

https://www.section.io/engineering-education/how-to-use-cors-in-nodejs-with-express/

https://levelup.gitconnected.com/getting-rid-of-origin-has-been-blocked-by-cors-policy-forever-969e161421ca

https://medium.com/@dtkatz/3-ways-to-fix-the-cors-error-and-how-access-control-allow-origin-works-d97d55946d9

https://enable-cors.org/server_expressjs.html

https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests

https://stackoverflow.com/questions/36878255/allow-access-control-allow-origin-header-using-html5-fetch-api

No comments:

Post a Comment