๋จผ์ cors์๋ฌ๋ผ๋์ง, ํน์ ๋คํธ์ํฌ ์๋ฌ๋ผ๋์ง ์ด๋๊ฐ ๋ฌธ์ ์ธ์ง ์ ํํ ํ์
์ด ์๋ ์ํ์ ์๋ฌ๋ผ๋ฉด, ๋์ ํ์ฌ ์ํฉ๊ณผ ์ด๋ฅผ ํด๊ฒฐํ๊ณ ์ ๋ฐฉ๋ฌธํ๋ ๋ง์ด ์ฌ์ดํธ์ ์ํฉ์ ๋น๊ตํด์ผ ํ๋๊ฒ ๋งค์ฐ ์ค์ํ ๊ฒ ๊ฐ์ต๋๋ค :)
๊ฐ์ ์๋ฌ๋ผ๋ ๋ฐ์์์ ์ด ๋ค ๋ค๋ฅด๋๊น์
๋ก์ปฌ์ 8999 ํฌํธ๋ก ์๋ฒ๋ฅผ ๊ตฌ๋ํ๊ณ ์์ต๋๋ค.
์๋ฒ๋ spring boot + jpa๋ฅผ ํ์ฉํ์ฌ ๋ง๋ค์ด ์คฌ๋๋ฐ์, ios ์๋ฎฌ๋ ์ดํฐ๊ฐ ์๋ expo ์ฑ์ ์ฌ์ฉํ์ฌ ์ง์ ํด๋ํฐ์ผ๋ก ์ ์์ ํ๋๋ฐ, ์๋ฒ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ํจ์นํ๋ ๊ณผ์ ์์ Axios Network ์๋ฌ๊ฐ ๋ฌ์ต๋๋ค.
์ผ๋จ ์๋ฒ์์ ๋ชจ๋ ๊ฒฝ๋ก์ ์์ฒญ์ ํ์ฉํ๋๋ก ์ค์ ํด ์ฃผ์์ต๋๋ค.
WebConfig.java
...
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*")
.allowedOriginPatterns("*")
.allowedHeaders("*")
.allowCredentials(true);
}
...
๋ณดํต ์ฑ์ ๋ง๋ค๊ฑฐ๋ ์น์ ๋ง๋ค๋, ํด๋น ๋ ธํธ๋ถ/๋ฉํ ์์ ์๋ฎฌ๋ ์ดํฐ๋ ์น์ ๋์์ ์์ ์ ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค๋ณด๋, axios url์์ localhost ๋ฅผ ์์ฑํ๋๊ตฐ์
[ ๋ณ๊ฒฝ ์ ]
function createInstaceWithAuth(url) {
return axios.create({
baseURL: `localhost:8999/${url}`,
responseType: 'json',
withCredentials: true,
})
}
์ด๋ ๊ฒ ํ๋ฉด ์ ํด๋ํฐ์ด localhost๋ฅผ ์ ์บ์นํ ๋ฆฌ๊ฐ ์์ฃ .
๊ทธ๋์ ํ์ฌ IP ์ฃผ์๋ฅผ ๋ฐ์, ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก baseURL์ ์์ฑํด์ฃผ๋๋ก ํ์ต๋๋ค!
[ ๋ณ๊ฒฝ ํ ]
import axios from 'axios'
import Constants from 'expo-constants' //ํ์ฌ ๋จ๋ง๊ธฐ์ ์์คํ
์ ๋ณด๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ํจ
const { manifest } = Constants
function createInstaceWithAuth(url) {
return axios.create({
baseURL: `http://${manifest.debuggerHost.split(':').shift()}:8999/${url}`,
responseType: 'json',
withCredentials: true,
})
}
[์ฐธ๊ณ ]
https://docs.expo.dev/versions/latest/sdk/constants/