Express CORS 에러 해결

·2024년 11월 10일
0

오류

목록 보기
6/10

CORS 에러 발생 배경

  • React, Express.js로 서버를 구성해서 로그인 요청 테스트 중인데, CORS 오류가 발생

    Access to fetch at 'http://localhost:4000/login' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

🤷‍♂️원인

  • CORS 정책 때문에 React앱(localhost:3000)에서 Express서버(localhost:4000)로 요청을 보낼 때 문제가 발생함. 브라우저 보안상 서로 다른 포트로 요청을 보낼 때,서버가 명시적으로 CORS 허용을 해줘야 함.

🔧해결방법

1.CORS 패키지 설치
npm install cors

2.서버에서 CORS설정

profile
냐홍

0개의 댓글