๐ node.js์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ Proisme ๊ธฐ๋ฐ http ํด๋ผ์ด์ธํธ
์ฆ, http๋ฅผ ์ด์ฉํด์ ์๋ฒ์ ํต์ ํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ํจํค์ง
๐ API ์๋ฒ๋ ์ฐ๋ฆฌ๊ฐ ๋ง๋ json-server๋ฅผ ์ฌ์ฉํ๋ค.
ํ ํ๋ก์ ํธ ๋ด์์ 4000 ๋ฒ ํฌํธ๋ก ์๋ฒ๋ฅผ ๊ฐ๋์์ผ ์ฌ์ฉํ๋ค.
yarn add axios
get์ ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์กฐํํ ๋ ์ฌ์ฉํ๋ค.
// url์๋ ์๋ฒ์ url์ด ๋ค์ด๊ฐ๊ณ , config์๋ ๊ธฐํ ์ฌ๋ฌ๊ฐ์ง ์ค์ ์ ์ถ๊ฐํ ์ ์๋ค.
axios.get(url[, config]) // GET
axios๋ get ์์ฒญ์ ํ ์ ์๋๋ก ๋์์ฃผ๋ ํจํค์ง์ผ ๋ฟ์ด์ง, ์ด๋ป๊ฒ ์์ฒญํด์ผํ์ง? ์ ๊ฐ์ ๋ฐฉ๋ฒ์ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ API ๋ช ์ธ์๋ฅผ ํ์ธํด์ผ ํ๋ค.
[json-server ๊ณต์๋ฌธ์]
์ ์ฒด ์ ๋ณด, ์์ธ ์ ๋ณด๋path variable
๋ก url์ ์์ฑํ๋ค.
filter์ ๊ฐ์ ๊ธฐ๋ฅ์ ์ํด์ GET ์์ฒญ์ ํ ๋๋query
๋ก ๋ณด๋ธ๋ค.
json-server์ ์๋ todos๋ฅผ axios๋ฅผ ์ด์ฉํด์ fetching ํ๊ณ useState๋ฅผ ํตํด ๊ด๋ฆฌํ๋ ๋ก์ง
// src/App.js
import React, { useEffect, useState } from "react";
import axios from "axios"; // axios import ํฉ๋๋ค.
const App = () => {
const [todos, setTodos] = useState(null);
// axios๋ฅผ ํตํด์ get ์์ฒญ์ ํ๋ ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
// ๋น๋๊ธฐ์ฒ๋ฆฌ๋ฅผ ํด์ผํ๋ฏ๋ก async/await ๊ตฌ๋ฌธ์ ํตํด์ ์ฒ๋ฆฌํฉ๋๋ค.
const fetchTodos = async () => {
const { data } = await axios.get("http://localhost:3001/todos");
setTodos(data); // ์๋ฒ๋ก๋ถํฐ fetchingํ ๋ฐ์ดํฐ๋ฅผ useState์ state๋ก set ํฉ๋๋ค.
};
// ์์ฑํ ํจ์๋ฅผ ์ปดํฌ๋ํธ๊ฐ mount ๋์ ๋ ์คํํ๊ธฐ ์ํด useEffect๋ฅผ ์ฌ์ฉํฉ๋๋ค.
useEffect(() => {
// effect ๊ตฌ๋ฌธ์ ์์ฑํ ํจ์๋ฅผ ๋ฃ์ด ์คํํฉ๋๋ค.
fetchTodos();
}, []);
// data fetching์ด ์ ์์ ์ผ๋ก ๋์๋์ง ์ฝ์์ ํตํด ํ์ธํฉ๋๋ค.
console.log(todos); // App.js:16
return <div>App</div>;
};
export default App;
post๋ ๋ณดํต ์๋ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ ๋ ์ฌ์ฉํ๋ค.
๋ณดํต์ ํด๋ผ์ด์ธํธ์ ๋ฐ์ดํฐ๋ฅผ body ํํ๋ก ์๋ฒ์ ๋ณด๋ด๊ณ ์ ํ ๋ ์ฌ์ฉํ๋ค.
axios.post(url[, data[, config]]) // POST
const onSubmitHandler = async(todo) => {
await axios.post("http://localhost:3001/todos", todo);
fetchTodos();
};
delete๋ ์ ์ฅ๋์ด ์๋ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํ๊ณ ์ ์์ฒญํ ๋ ์ฌ์ฉํ๋ค.
axios.delete(url[, config]) // DELETE
const onClickDeleteButtonHandler = (todoId) => {
axios.delete(`http://localhost:3001/todos/${todoId}`);
setTodos(todos.filter((item) => item.id !== id));
};
patch๋ ๋ณดํต ์ด๋ค ๋ฐ์ดํฐ๋ฅผ ์์ ํ๊ณ ์ ์๋ฒ์ ์์ฒญ์ ๋ณด๋ผ ๋ ์ฌ์ฉํ๋ ๋ฉ์๋์ด๋ค.
axios.patch(url[, data[, config]]) // PATCH
const onUpdateButtonClickHandler = async () => {
await api.patch(`http://localhost:3001/todos/${targetId}`, {
title: content,
});
setTodos(
todos.map((item) => {
if (item.id == targetId) return { ...item, title: content };
else return item;
})
);
};
fetch
๋ ES6 ๋ถํฐ ๋์
๋ Javasciprt ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด๋ค. (Promise ๊ธฐ๋ฐ ๋น๋๊ธฐ ํต์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ)
๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด๊ธฐ ๋๋ฌธ์ ๋ณ๋์ ์ค์น ๋ฐ import ๊ฐ ํ์ ์๋ค.
๊ทธ๋ฌ๋ ์ฐ๋ฆฌ๋ fetch
๊ฐ ๊ฐ์ง ๋จ์ ๋ค ๋๋ฌธ์ axios๋ฅผ ์ฃผ๋ก ์ฌ์ฉํ๋ค.
[fetch์ ๋จ์ ]
1. ๋ฏธ์ง์ ๋ธ๋ผ์ฐ์ ์กด์ฌ
2. ๊ฐ๋ฐ์์๊ฒ ๋ถ์น์ ํ response
3. axios์ ๋นํด ๋ถ์กฑํ ๊ธฐ๋ฅ
const url = "https://jsonplaceholder.typicode.com/todos";
fetch(url)
.then((response) => response.json())
.then(console.log);
fetch().then ์ ํ ์ํ์ฌ๋ ์ฌ์ ํ JSON ํ์์ด ์๋๊ธฐ ๋๋ฌธ์ response.json()์ ํ๋ฒ ๋ ํด์ฃผ๋ ๊ณผ์ ์ด ํ์ํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก fetch๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๋ ๊ฒฝ์ฐ ๋ ๊ฐ์ then
์ด ํ์ํ๋ค.
const url = "https://jsonplaceholder.typicode.com/todos";
axios.get(url).then((response) => console.log(response.data));
axios๋ ์น์ ํ๊ฒ๋ ์๋ต(response)๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก JSON ํฌ๋งท์ผ๋ก ์ ๊ณตํ๋ค.
์ฐ๋ฆฌ๋ ๋จ์ํ response.data๋ก๋ง ์ฌ์ฉํ๋ฉด ๋๋ค.
const url = "https://jsonplaceholder.typicode.com/todos";
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(
`This is an HTTP error: The status is ${response.status}`
);
}
return response.json();
})
.then(console.log)
.catch((err) => {
console.log(err.message);
});
fetch๋ catch๊ฐ ์ดํํ๋ ๊ฒฝ์ฐ๋ ์ค์ง ๋คํธ์ํฌ ์ฅ์ ์ผ์ด์ค์ด๋ค.
๋ฐ๋ผ์ ๊ฐ๋ฐ์๊ฐ ์ผ์ผ์ด then() ์์ ๋ชจ๋ ์ผ์ด์ค์ ๋ํ HTTP ์๋ฌ ์ฒ๋ฆฌ๋ฅผ ํด์ผ ํ๋ค.
const url = "https://jsonplaceholder.typicode.com/todos";
axios
.get(url)
.then((response) => console.log(response.data))
.catch((err) => {
console.log(err.message);
});
axios.get() ์์ฒญ์ด ๋ฐํํ๋ Promise ๊ฐ์ฒด๊ฐ ๊ฐ์ง๊ณ ์๋ ์ํ์ฝ๋๊ฐ 2XX ๋ฅผ ๋์ด๊ฐ๋ฉด ๊ฑฐ๋ถ (reject)๋ฅผ ํ๋ค.
๋ฐ๋ผ์ ๊ณง๋ฐ๋ก catch() ๋ถ๋ถ์ ํตํด error handling
์ด ๊ฐ๋ฅํ๋ค.
const url = "https://jsonplaceholder.typicode.com/todos";
// axios ์์ฒญ ๋ก์ง
axios
.get(url)
.then((response) => console.log(response.data))
.catch((err) => {
// ์ค๋ฅ ๊ฐ์ฒด ๋ด์ response๊ฐ ์กด์ฌํ๋ค = ์๋ฒ๊ฐ ์ค๋ฅ ์๋ต์ ์ฃผ์๋ค
if (err.response) {
const { status, config } = err.response;
// ์๋ ํ์ด์ง
if (status === 404) {
console.log(`${config.url} not found`);
}
// ์๋ฒ ์ค๋ฅ
if (status === 500) {
console.log("Server error");
}
// ์์ฒญ์ด ์ด๋ฃจ์ด์ก์ผ๋ ์๋ฒ์์ ์๋ต์ด ์์์ ๊ฒฝ์ฐ
} else if (err.request) {
console.log("Error", err.message);
// ๊ทธ ์ธ ๋ค๋ฅธ ์๋ฌ
} else {
console.log("Error", err.message);
}
});