NodeJS with CORS
Enable CORS in express.js
As node.js is often used to build API, proper CORS setting can be a life saver if you want to be able to request the API from different domains.
In the exemple, we'll set it up for the wider configuration (authorize all request types from any domain.
In your server.js after initializing express:
Usually, node is ran behind a proxy on production servers. Therefore the reverse proxy server (such as Apache or Nginx) will be responsible for the CORS config.
To conveniently adapt this scenario, it's possible to only enable node.js CORS when it's in development.
This is easily done by checking NODE_ENV: