<aside> ๐ก fetch๋? fetch๋ ๋น๋๊ธฐ ์ ์ผ๋ก api๋ฅผ ๋ถ๋ฌ์ค๊ณ , ์ ๋ณด๋ฅผ ๋ด๋ณด๋ด ์ฃผ๊ธฐ๋ ํ๋ ํจ์์ด๋ค. fetch ํจ์์ ์ฐ์ฌ์ง๋ method๋ get๊ณผ post๊ฐ ์๋๋ฐ ์ค์ ์ ๋ฐ๋ก ์ํด์ฃผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก get์ผ๋ก ์ค์ ์ด ๋์ด์๋ค.
</aside>
async function request() {
const response = await fetch('<https://api.github.com/orgs/react>',
{
method: 'GET', //method ์ง์ ์ ํ์ง ์์ ๊ฒฝ์ฐ์ default ๊ฐ์ GET์ด๋ค.
});
const data = await response.json();
console.log(data) // data๋ฅผ ์๋ต ๋ฐ์ ํ์ ๋ก์ง
}
request();
function request() {
fetch('<https://api.github.com/orgs/react>', {
method: 'GET', //method ์ง์ ์ ํ์ง ์์ ๊ฒฝ์ฐ์ default ๊ฐ์ GET์ด๋ค.
})
.then(response => {
return response.json();
})
.then(data => {
console.log(data); // data๋ฅผ ์๋ต ๋ฐ์ ํ์ ๋ก์ง
});
}
request();
<aside> ๐ก axios๋?
Node.js์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ Promise API๋ฅผ ํ์ฉํ๋ HTTP ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค. ๋น๋๊ธฐ๋ก HTTP ํต์ ์ ํ ์ ์์ผ๋ฉฐ return์ promise ๊ฐ์ฒด๋ก ํด์ฃผ๊ธฐ ๋๋ฌธ์ response ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๊ธฐ ์ฝ๋ค.
</aside>