유통기한 관리_개인프로젝트

CORS for multiple origins

avocado12 2021. 4. 20. 16:48

cors npm 패키지는 origin value에 대한 함수를 작성하는 옵션을 제공한다.

여러 도메인에 대해 CORS를 활성화하는 데 도움이 된다.

const cors = require('cors');
const allowedDomains = ["http://localhost:3000", "https://d-day.netlify.app"]
app.use(cors({
  origin: function(origin, callback) {
    if(!origin) return callback(null, true);
    if(allowedDomains.indexOf(origin) === -1) {
      const msg = `This site ${origin} does not have an access. Only specific domains are allowed to access it.`;
      return callback(new Error(msg), false);
    }
    return callback(null, true);
  }
}));