Last active
May 31, 2020 14:59
-
-
Save hmmhmmhm/3021b086aa65116d06972b4302a1797a to your computer and use it in GitHub Desktop.
Code to allow CORS only for valid domain patterns, 유효한 도메인 패턴만 CORS 허용하는 코드
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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