CORS 란?
Cross Origin Resource Sharing
브라우저가 웹 애플리케이션에 선택된 접근 권한을 부여하는 메커니즘이다.
서로 다른 출처의 리소스를 요청하는 경우, 보안 문제로 이를 차단한다.
해결방법
1. 동일한 출처 사용하기
같은 주소 내에서 데이터를 주고 받는 경우, 오류가 발생하지 않는다.
2. 서버
HTTP 응답 헤더에서 설정을 변경할 수 있다.
express 모듈을 사용하는 경우,
const express = require("express");
const cors = require("cors");
const app = express();
app.use(
cors({
origin: "[주소]",
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
})
);
3. 클라이언트
리소스를 요청하는 서버 사이테 프록시 서버를 만들어 사용하는 방법이 있다.
- 프록시 서버 : 브라우저와 서버를 통신하는 과정 중간에서 정보 교환을 도와주는 중간 서버를 말한다.
Vite + ts
vite.config.ts 파일에 proxy 내용 추가하기 (client -> server 방향!!)
export default defineConfig({
plugins: [react()],
server:{
proxy: {
"[경로]": {
target: "[주소]",
changeOrigin: true,
secure: false,
ws: true,
}
}
}
}
Configuring Vite | Vite
Configuring Vite Config File Config File Resolving When running vite from the command line, Vite will automatically try to resolve a config file named vite.config.js inside project root. The most basic config file looks like this: Note Vite supports using
vitejs.dev
CRA
[Typescript] CRA에서 http-proxy-middleware 사용법
[Typescript] CRA에서 http-proxy-middleware 사용법
React에서 상대경로에 대해 프록시를 통해서 Origin을 바꿔서 보낼 수 있다. 상대경로는 package.json의 옵션으로도 설정할 수 있지만, 오늘은 CRA 공식 홈페이지에서 소개하고 있는 http-proxy-middleware 에
egas.tistory.com
참고자료
CORS 와 해결방법 (express, proxy) - 네트워크 study2
CORS 와 해결방법 (express, proxy) - 네트워크 study2
1. CORS란? 같은 주소, 같은 포트에 있는 리소스를 불러올 때는 문제가 없지만 다른 출처의 리소스를 요청하게 되면 보안적인 문제로 기본적으로 이를 차단함. CORS (Cross Origin Resource Sharing) : HTTP 헤
velog.io
'Node.js' 카테고리의 다른 글
[Node] OkPacket 로그 지우기 (0) | 2022.07.06 |
---|---|
[Node] nodemailer로 이메일 전송하기 (0) | 2022.05.29 |
[42 OAuth] The redirect uri included is not valid. (0) | 2022.01.13 |
NodeJS + Typescript 환경 세팅하기 (0) | 2021.12.19 |
[node.js] express + passport + 42 API 로그인 구현 (0) | 2021.10.29 |