๐ฅ ์๋ฒ ์ชฝ ํฌํธ๋ 8080 / front ์ชฝ ํฌํธ๋ 3000์ ์ฐ๊ณ ์๋ค.
๐ข back : springboot java 11
๐คข front : react node
8080 ์๋ฒ๋ก API ์์ฒญ์ ์์ฒญ๊ฐ์ ๋ค์๊ณผ ๊ฐ์ด ์จ๋ค.
back๋จ์ response header์ Access-Control-Allow-Origin ์ค์ ์ ํ๋ค๋ฉด,
๋ชจ๋ ์์ฒญ response์ header๋ฅผ ์ค์ ํด์ผ ํ๋ค.
๋ก์ปฌํ๊ฒฝ์์๋ ์ฒ๋ฆฌํ์ง๋ง,
์ํ ๋ฐฐํฌ์ดํ์๋ nginx ๋ apache์ proxy ์ค์ ์ด ํ์ํ๋ค.
๐คจ ํด๋ผ์ด์ธํธ์ชฝ์ proxy ์ค์ ์ ํตํด ํด๊ฒฐ ํ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ๋ค.
1. ๋ฏธ๋ค์จ์ด ์ค์น๋ก ์ค์
$ npm install http-proxy-middleware --save
$ yarn add http-proxy-middleware
์ค์ ์ ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํ๋ค.
Proxying API Requests in Development | Create React App
Note: this feature is available with react-scripts@0.2.3 and higher.
create-react-app.dev
src/setupProxy.js
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/walk',
createProxyMiddleware({
target: 'http://localhost:8080',
changeOrigin: true,
})
);
};
API ์์ฒญ ๊ฒฝ๋ก์ /api ๋ฅผ ์ฌ์ฉํ ๋ค๋ฉด http://localhost:8080 ํธ์คํธ๋ก ์์ฒญ ์ ๋ฌ๋๋ค.
back ๋จ์์ ์ธ์ฆํค๋ง ๋์ด์ค๋ฉด home์ผ๋ก ์ด๋ํ๋๋ก ๊ฐ๋จํ ๊ตฌํํด๋ณด์๋ค.
defaultAxios.post("walk/login",
{
// email:data.get(`email`),
email:data.get(`email`),
password:data.get(`password`)
},
{headers: { "Content-Type": `application/json`}})
.then(response => {
console.log(response," ํค๋๊ฐ : ",response.headers.authrozation);
if (response.headers.authrozation !==null) {
document.location.href = '/home'
}
}).catch( err =>{
console.log("ERROR: ", err)
});
๋ก๊ทธ์ธ ์ ๋ค์๊ณผ ๊ฐ๋ค.
2. package.json ์ค์ .
๊ฐ๋จํ๋ค๋ ์ฅ์ ์ด์๋ค.
** CRA( create-react-app ) ์ ์ด์ฉํ ๋ฆฌ์กํธ ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ ๊ฒฝ์ฐ + 0.2.3 ๋ฒ์ ์ด์์ธ ๊ฒฝ์ฐ ์ฌ์ฉ
** ํ๋ก์ ์ค์ ์ด ์ ์ญ์ผ๋ก ์ ์ฉ๋๋ค.
** react dev server ๊ฐ response๋ฅผ browser์ ์ ๋ฌํ์ง๋ง, websocket ์์ฒญ์ protocol์ด http๋ก ๋ฐ๋๋ค๋ ๋ฌธ์ ๊ฐ ์๋ค.
package.json
}
ใปใปใปใป
"proxy": "http://localhost:8080"
}