Node.js

axios 요청 url에 prefix값을 주는 법

pangyoelon 2023. 3. 23. 19:16

상황)

axios.get('http://localhost:5000')
axios.post('http://localhost:5000','data')
axios.get('http://localhost:5000')
axios.post('http://localhost:5000','data')
axios.get('http://localhost:5000')
axios.post('http://localhost:5000','data')
axios.get('http://localhost:5000')
axios.post('http://localhost:5000','data')
axios.get('http://localhost:5000')
axios.post('http://localhost:5000','data')

생활코딩식 상상력을 발휘하여 위와 같은 요청이 1억개가 있다고 하자

 

그럼 당연히 엄청난 중복이 발생할 것이다

 

아래와 같이 axios.defaults.baseURL에 원하는 prefix값을 설정해주면 중복이 줄어들 수 있다

axios.defaults.baseURL = 'http://localhost:5000'

axios.get('/')
axios.post('/','data')
axios.get('/')
axios.post('/','data')
axios.get('/')
axios.post('/','data')
axios.get('/')
axios.post('/','data')
axios.get('/')
axios.post('/','data')

 

 

 

 

+) chatgpt는 신이다 (아래 방법은 위와 조금 다름)