Enable CORS for all requests from a certain domain in Express.js/Node.js application?

nick asked this question 21 days ago
nick 21 days ago · Edited

In my Express.js/Node.js application, how can I enable CORS request from any URLs from a given domain name?

My current CORS configuration:

app.use(cors({origin: "MY_URL"}))

I'm using the cors NPM package to configure CORS settings for my application.

2 suggested answers
itsbambi 13 days ago

Update your origin setting to this:

  origin: /domain\.com$/

This would configure your Access-Control-Allow-Origin CORS header for any request with the domain.com domain.

So, requests from api.domain.com, assets.domain.com, etc. would work.

0 replies
yaboy01 13 days ago

You can use a regular expression:

origin: /my_domain\.com$/

Or you can also create an array of accepted URLs:

origin: ["https://my_url.com", "https://my_url_2.com", "https://my_url_3.com"]
0 replies