Answered
In my Node.js/Express.js application, I have CORS enabled for the URL of my frontend website.
My CORS configuration code looks like this right now:
app.use(cors({origin: "https://myURL.com}))
How do I add more than one URL in the CORS settings?
You can create a whitelist array like this:
var whitelist = ["https://example1.com", "https://example2.com"]
var corsOptions = {
origin: function (origin, callback) {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error("Not allowed by CORS"))
}
}
}
This will allow CORS for both of the URLs in the whitelist
array.
This is an example they give in their documentation.
You can use a regular expression for the origin
option:
origin: /domain\.com$/
This will allow CORS from all URLs with the domain.com
domain.