Skip to content

Instantly share code, notes, and snippets.

@hmmhmmhm
Last active May 31, 2020 14:59
Show Gist options
  • Save hmmhmmhm/3021b086aa65116d06972b4302a1797a to your computer and use it in GitHub Desktop.
Save hmmhmmhm/3021b086aa65116d06972b4302a1797a to your computer and use it in GitHub Desktop.
Code to allow CORS only for valid domain patterns, 유효한 도메인 패턴만 CORS 허용하는 코드
import matcher from 'matcher'
import CORS from 'cors'
let corsOptions = {
origin: (origin, callback) => {
let allow = false
for (let allowAccessDomain of settingsData.allowAccessDomains) { // [ "*.mydomain.com"]
if (matcher.isMatch(origin, allowAccessDomain)) {
allow = true
break
}
}
if (allow) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
}
// * Enable CORS
expressInstance.use(CORS(corsOptions))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment