Today I Learned
CORS cross domain issue
c3epmos
2020. 1. 15. 17:21
문제
react.js 클라이언트로부터 node.js로 구축한 API 서버에 서비스를 요청할 때, CORS cross domain issue가 발생했다. 이 문제는 다른 도메인 서버에 요청하는 것을 보안 문제로 간주해 이를 막는 것이다.
해결
이 문제는 서버측에서 CORS를 활성화시켜 cross domain을 허용해 해결할 수 있다.
app.js
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 = require('cors'); | |
| /* ... */ | |
| let corsOptions = { | |
| origin: '*', | |
| credentials: true | |
| } | |
| app.use(cors(corsOptions)); |
origin은 허용하고자 하는 주소를 뜻하며, credentials는 true로 설정하면 모든 res 헤더에 추가해준다.