✅ Axios
- javascript에서 사용할 수 있는 HTTP 클라이언트 라이브러리
- 해당 라이브러리를 사용하여 서버와 통신, 데이터를 가져오고 보낼 수 있음
- 브라우저와 Node.js에서 모두 사용 가능
- Promise 기반 Api를 사용
- 다양한 HTTP 요청 메서드 지원 (ex. GET, POST, PUT, DELETE)
- 다양한 응답 데이터 형식 지원 (ex. JSON, XML, FormData)
- 직관적인 Api, 다양한 기능, 브라우저와 Node.js에서 모두 사용할 수 있어 많은 개발자들이 선호
📨 HTTP 메서드
1. GET : 서버에서 리소스를 읽어오기 위해 사용
axios.get('/api/users', {headers})
.then(response => {
console.log(response.data);
}
.catch(error => {
console.log(error);
}
2. POST : 클라이언트가 서버에 데이터를 보내기 위해 사용
- 두 번째 인자로 데이터 객체를 넘겨줄 수 있음
axios.post('/api/users', {
name: 'ahyun',
email: 'ahyun@ahyun.com'
}, {headers})
.then(response => {
console.log(response.data);
}
.catch(error => {
console.log(error);
}
3. PUT : 서버에 새로운 데이터를 저장하거나 기존 데이터를 업데이트할 때 사용
- 두 번째 인자로 데이터 객체를 넘겨줄 수 있음
axios.put('/api/users/1', {
name: 'ahyun',
email: 'ahyun@ahyun.com'
}, {headers})
.then(response => {
console.log(response.data);
}
.catch(error => {
console.log(error);
}
4. DELETE : 서버에서 리소스를 삭제하기 위해 사용
axios.delete('/api/users/1', {headers})
.then(response => {
console.log(response.data);
}
.catch(error => {
console.log(error);
}