CRA로 만든 프로젝트에서 프록시를 설정할 때는 package.json 파일을 수정하면 됩니다. 해당 파일을 열어서 다음 내용을 추가하세요.
package.json
{
"name": "blog-frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
(...)
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"proxy": "http://localhost:4000/"
}
이제 리액트 애플리케이션에서 client.get('/api/posts')를 하면, 웹팩 개발 서버가 프록시 역할을 해서 http://localhost:4000/api/posts에 대신 요청한 뒤 결과물을 응답해 줍니다.