axios는
지원하는 브라우저가 더 많고
에러핸들링이 좋고
자동으로 데이터가 json형태로 되어 넘어간다
Automatic JSON data transformation
As we saw earlier, Axios automatically stringifies the data when sending requests (though you can override the default behavior and define a different transformation mechanism). When using fetch(), however, you’d have to do it manually.
Compare:
// axios
axios.get('https://api.github.com/orgs/axios')
.then(response => {
console.log(response.data);
}, error => {
console.log(error);
});
// fetch()
fetch('https://api.github.com/orgs/axios')
.then(response => response.json()) // one extra step
.then(data => {
console.log(data)
})
.catch(error => console.error(error));
fetch는 직접 json화를 해줘야하지만
지원하는 브라우저 문제 등을 충분히 해결할 수 있다.
결론
Axios는 대부분의 HTTP통신 요구사항을 위해 컴팩트 패키지로 사용하기 쉬운 API를 제공한다.
하지만 fetch()메소드를 사용해서도 Axios 라이브러리의 주요기능을 완벽하게 재현할 수 있다.
는 글
https://blog.logrocket.com/axios-vs-fetch-best-http-requests/
Axios vs. fetch(): Which is best for making HTTP requests? - LogRocket Blog
Axios is not always an ideal solution; depending on your needs, there are sometimes better options for making HTTP requests. The Fetch API is one of them.
blog.logrocket.com
'노드 node.js' 카테고리의 다른 글
Cannot set headers after they are sent to the client 에러 (0) | 2023.06.14 |
---|---|
nodejs 가 뭐야? (feat. express) (0) | 2023.02.19 |
formdata로 id,password,profile(이미지) mySQL 저장 성공 (0) | 2022.05.02 |
bodyParser써야하는 이유 (0) | 2022.05.02 |
formData 이미지데이터 보내고 받기 (react,nodejs,header) (0) | 2022.04.28 |