Created
April 25, 2019 01:17
-
-
Save peterkimzz/93888e883944717e5f9d97ccf78a7f18 to your computer and use it in GitHub Desktop.
nodejs 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
| const cors = (req, res, next) => { | |
| // 와일드카드 (*)가 안먹혀서 일일이 배열안에 적어주면 됨 | |
| const allowedOrigins = [ | |
| 'http://localhost:3000', | |
| 'http://localhost:3001', | |
| 'http://localhost:3002', | |
| 'http://localhost:3003', | |
| 'https://test.domain.com', | |
| 'https://domain.com', | |
| 'https://www.domain.com', | |
| 'https://admin.domain.com' | |
| ] | |
| const origin = req.headers.origin | |
| if (allowedOrigins.indexOf(origin) > -1) { | |
| res.setHeader('Access-Control-Allow-Origin', origin) | |
| res.setHeader('Access-Control-Allow-Credentials', true) | |
| } | |
| res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE') | |
| res.header( | |
| 'Access-Control-Allow-Headers', | |
| 'Content-Type,X-Requested-With,x-access-token' | |
| ) | |
| res.header('Access-Control-Allow-Credentials', true) | |
| next() | |
| } | |
| module.exports = cors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment