๋น๋๊ธฐ ์์ฒญ์ ๋ํ์ ์ธ ์ฌ๋ก์๋ ๋คํธ์ํฌ ์์ฒญ์ด ์๋ค.
๊ทธ์ค์์ URL๋ก ์์ฒญํ๋ ๊ฒฝ์ฐ๊ฐ ๊ฐ์ฅ ํํ๋ฐ,
URL๋ก ์์ฒญํ๋ ๊ฒ์ ๊ฐ๋ฅํ๊ฒ ํด์ฃผ๋ API๊ฐ ๋ฐ๋ก fetch API์ด๋ค.
โ๏ธ์ฐธ๊ณ ) fetch๋ Promise์ ๊ฐ์ฒด ํ์
์ ๋ฐํํฉ๋๋ค.
fetch('http://example.com/movies.json')
.then((response) => response.json())
.then((json) => console.log(json));
.catch((error) => console.error(error));
์ ํ์ ์ผ๋ก ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๊ณตํ๋ค. (ํ์๊ฐ ์๋)
const data = { username: 'example' };
fetch('https://example.com/profile', {
method: 'POST', // ๋๋ 'PUT', 'GET'
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then((response) => response.json())
.then((json) => {
console.log('์ฑ๊ณต:', json);
})
.catch((error) => {
console.error('์คํจ:', error);
});
GET : ์ ๋ณด๋ฅผ ์์ฒญํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
// Promise ver
fetch('https://koreanjson.com/users/1', { method: 'GET' })
.then((response) => response.json())
.then((json) => console.log(json))
.catch((error) => console.log(error));
// Async / Await ver
async function request() {
const response = await fetch('https://koreanjson.com/users/1', {
method: 'GET',
});
const data = await response.json();
console.log(data);
}
POST : ์๋ฒ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
// Promise ver
fetch('https://koreanjson.com/users', {
method: 'POST',
headers: {
// JSON์ ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด์ค๋ค๊ณ ์๋ฒ์๊ฒ ์๋ ค์ฃผ๋ ์ญํ ์
๋๋ค.
'Content-Type': 'application/json',
},
body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
})
.then((response) => response.json())
.then((json) => console.log(json))
.catch((error) => console.log(error));
// Async / Await ver
async function request() {
const response = await fetch('https://koreanjson.com/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ nickName: 'ApeachIcetea', age: 20 }),
});
const data = await response.json();
console.log(data);
}
request();
Axios๋ ๋ธ๋ผ์ฐ์ , Node.js๋ฅผ ์ํ Promise API๋ฅผ ํ์ฉํ๋ HTTP ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
Fetch API๋ณด๋ค ์ฌ์ฉ์ด ๊ฐํธํ๋ฉด์ ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ๋ค์ด ํฌํจ๋์ด์๋ค.
๊ทธ๋์ ์ค์ ๋ก๋ Fetch๋ณด๋ค Axios๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
โ๏ธ์ฐธ๊ณ ) HTTP๋ 'HyperText Transfer Protocol'์ ์ฝ์๋ก ํ์ดํผ ํ
์คํธ๋ฅผ ๊ตํ, ์ ์ก์ ์ํ ํต์ ๊ท์ฝ์
๋๋ค.
npm install axios
//โ๏ธ์ฐธ๊ณ ) ๊ธ๋ก๋ฒ ์ค์นX, ์ฌ์ฉํ๊ณ ์ถ์ ํด๋์ ๊ฐ๋ณ๋ก ์ค์นํ๋ฉด ๋๋ค.
axios
.get('https://koreanjson.com/users/1')
.then((response) => {
const { data } = response;
console.log(data);
})
.catch((error) => console.log(error));
์ ํ์ ์ผ๋ก ๋ ๋ฒ์งธ ๋งค๊ฐ๋ณ์๋ฅผ ์ ๊ณตํ๋ค. (ํ์๊ฐ ์๋)
const data = { username: 'example' };
axios
.post('https://example.com/profile', data) // ๋๋ get, put
.then((response) => {
const { data } = response;
console.log('์ฑ๊ณต:', data);
})
.catch((error) => {
console.error('์คํจ:', error);
});
GET : ์ ๋ณด๋ฅผ ์์ฒญํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
axios.get("url"[,config])
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
import axios from 'axios';
// Promise ver
axios
.get('https://koreanjson.com/users/1')
.then((response) => {
const { data } = response;
console.log(data);
})
.catch((error) => console.log(error));
// Async / Await ver
async function request() {
const response = await axios.get('https://koreanjson.com/users/1');
const { data } = response;
console.log(data);
}
request();
POST : ์๋ฒ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ธฐ ์ํด ์ฌ์ฉ๋๋ ๋ฉ์๋
axios.post("url"[, data[, config]])
// axios๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์ค์นํ axios๋ฅผ ๋ถ๋ฌ์์ผ ํฉ๋๋ค.
import axios from 'axios';
// Promise ver
axios
.post('https://koreanjson.com/users', { nickName: 'ApeachIcetea', age: '20' })
.then((response) => {
const { data } = response;
console.log(data);
})
.catch((error) => console.log(error));
// Async / Await ver
async function request() {
const response = await axios.post('https://koreanjson.com/users', {
name: 'ApeachIcetea',
age: '20',
});
const { data } = response;
console.log(data);
}
request();
Fetch API
๋นํธ์ธ API๋ผ ๋ณ๋์ ์ค์น๊ฐ ํ์์๋ค.
.json() ๋ฉ์๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
Axios
์จ๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ค์น๊ฐ ํ์ํ๋ค.
์๋์ผ๋ก JSON๋ฐ์ดํฐ ํ์์ผ๋ก ๋ณํ๋๋ค.
๋ฉ์๋ | ์ญํ | ๋ฉฑ๋ฑ์ฑ |
---|---|---|
POST | Create | NO |
PATCH | ์์ (์์ฒญํ ๋ถ๋ถ๋ง ๋ฐ์๋๋ฉฐ ๋๋จธ์ง๋ ๊ธฐ์กด์ ๋ฐ์ดํฐ๊ฐ ์ ์ง) | NO |
PUT | Update (๋ณ๊ฒฝ๋์ง ์๋ ๋ฐ์ดํฐ๋ ๋ชจ๋ ์ ๋ฌํด์ผ ํจ) | YES |
GET | Read | YES |
DELETE | Delete | YES |
โ๏ธ์ฐธ๊ณ ) ๋ฉฑ๋ฑ์ฑ์ด๋ ์ฌ๋ฌ๋ฒ ์ํํด๋ ๊ฒฐ๊ณผ๊ฐ ๊ฐ์์ ์๋ฏธํ๋ค.
โ ํท๊ฐ๋ฆฌ๋ PATCH์ PUT์ ๋ฉฑ๋ฑ์ฑ
PUT์ ๋ฐ์ดํฐ๋ฅผ ์์ ํ ๊ต์ฒดํด ๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์ ๋ฉฑ๋ฑ์ด๊ณ ,
PATCH๋ ๋ฉฑ๋ฑ์ผ์๋, ๋ฉฑ๋ฑ์ด ์๋์๋ ์๋ค.
(์๋ฅผ ๋ค์ด PATCH๋ก ๋จ์ํ ์ ์ ์ ์ด๋ฆ์ ๋ฐ๊พธ๋ ๊ฒ์ ๋ฉฑ๋ฑํ์ง๋ง,
ํธ์ถํ ๋๋ง๋ค +1์ด ๋๋ค๋ฉด ๋ฉฑ๋ฑํ์ง ์์ ๊ฒ์ด ๋๋ค.)