๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
ํ๋ก ํธ์์ ์๋ฒ์ ๋ฐ์ดํฐ ์์ฒญํ๊ธฐ (React๋ก fetch, axios ์ฌ์ฉ) ๋ณธ๋ฌธ
๐ฉ๐ป ๋ฐฑ์๋(Back-End)/Node js
ํ๋ก ํธ์์ ์๋ฒ์ ๋ฐ์ดํฐ ์์ฒญํ๊ธฐ (React๋ก fetch, axios ์ฌ์ฉ)
์ง์ง์ํ์นด 2023. 5. 18. 18:59728x90
๋ฐ์ํ
<๋ณธ ๋ธ๋ก๊ทธ๋ ๋ผ๋งค๊ฐ๋ฐ์ ์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> ํ๋ก ํธ์์ ์๋ฒ์ ๋ฐ์ดํฐ ์์ฒญํ๋ ๋ฐฉ๋ฒ (React๋ก fetch, axios ์ฌ์ฉํ๊ธฐ)
๐ SERVER
const express = require("express");
const app = express();
// body-parser
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// ๋ฐ์ดํฐ๋ฅผ ์๋ฒ์ ์ ์ฅํด๋์ (๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ต)
let id = 2;
const todoList = [
{
id: 1,
text: "ํ ์ผ 1",
done: false,
},
];
app.get("/", function (res, res) {
res.send("server");
});
// todolist ์กฐํ
app.get("/api/todo", (req, res) => {
res.json(todoList);
});
// todolist ๋ฑ๋ก
app.post("/api/todo", (req, res) => {
// body ์ ๋ฐ์ดํฐ ๋ฃ์ด ์ ์ก
const { text, done } = req.body;
todoList.push({
id: id++,
text,
done,
});
return res.send("success");
});
app.listen(8080, () => {
console.log("listen on 8080");
});
๐ท todoList ์กฐํํ๊ธฐ
๐ท todoList ๋ฑ๋กํ๊ธฐ
๐ CLIENT
๐ทfetch ๋ก ๋ฐ์ดํฐ ์์ฒญํ๊ณ ๋ฐ์์ด client/src/App.js
import { useEffect, useState } from "react";
// ๊ธฐ๋ณธ ์ ๊ณตํ๋ api๋ fetch
// axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ
// ํ๋ก ํธ๊ฐ ์๋ฒ์ ๋ฐ์ดํฐ ์์ฒญํ๋ ค๋ฉด "์๋ฒ์ฃผ์", "http ๋ฉ์๋" ํ์
function App() {
const [todoList, setTodoList] = useState([]);
useEffect(() => {
fetchData();
}, []);
// fetch ์ด์ฉํ์ฌ ๋ฐ์ดํฐ ์๋ต ๋ฐ๊ธฐ
const fetchData = () => {
fetch("http://localhost:8080/api/todo")
.then((response) => response.json())
.then((data) => setTodoList(data));
};
// ๋ฐ์ดํฐ ์ถ๊ฐํ๊ณ ์๋ฒ์ POST ์์ฒญ
const onSubmitHandler = (e) => {
e.preventDefault();
const text = e.target.text.value;
const done = e.target.done.checked;
fetch("http://localhost:8080/api/todo", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
text,
done,
}),
}).then(() => fetchData());
};
return (
<div className="App">
<h1>TODO LIST</h1>
<form onSubmit={onSubmitHandler}>
<input name="text" />
<input name="done" type="checkbox" />
<input type="submit" value="์ถ๊ฐ" />
</form>
{/* ์กฐํํ๋ ๊ณณ */}
{todoList.map((todo) => (
<div key={todo.id} style={{ display: "flex" }}>
<div>{todo.id}</div>
<div>{todo.text}</div>
<div>{todo.done ? "Y" : "N"}</div>
</div>
))}
</div>
);
}
export default App;
๐ทaxios ๋ก ๋ฐ์ดํฐ ์์ฒญํ๊ณ ๋ฐ์์ด client/src/App.js
import { useEffect, useState } from "react";
import axios from "axios";
// ๊ธฐ๋ณธ ์ ๊ณตํ๋ api๋ fetch
// axios ๋ผ์ด๋ธ๋ฌ๋ฆฌ
const SERVER_URL = "http://localhost:8080/api/todo";
// ํ๋ก ํธ๊ฐ ์๋ฒ์ ๋ฐ์ดํฐ ์์ฒญํ๋ ค๋ฉด "์๋ฒ์ฃผ์", "http ๋ฉ์๋" ํ์
function App() {
const [todoList, setTodoList] = useState([]);
// axios ์ด์ฉํ์ฌ ๋ฐ์ดํฐ ์๋ต ๋ฐ๊ธฐ
const fetchData = async () => {
// ์๋ต ๋ณด๋ด๊ธฐ -> ์๋ต ์ค๋ฉด setTolist์ ๋ฃ๊ธฐ
const response = await axios.get(SERVER_URL);
setTodoList(response.data);
};
useEffect(() => {
fetchData();
}, []);
// ๋ฐ์ดํฐ ์ถ๊ฐํ๊ณ ์๋ฒ์ POST ์์ฒญ
const onSubmitHandler = async (e) => {
e.preventDefault();
const text = e.target.text.value;
const done = e.target.done.checked;
// axios ์ด์ฉํ์ฌ ๋ฐ์ดํฐ ๋ณด๋ด๊ธฐ
await axios.post(SERVER_URL, { text, done });
fetchData();
};
return (
<div className="App">
<h1>TODO LIST</h1>
<form onSubmit={onSubmitHandler}>
<input name="text" />
<input name="done" type="checkbox" />
<input type="submit" value="์ถ๊ฐ" />
</form>
{/* ์กฐํํ๋ ๊ณณ */}
{todoList.map((todo) => (
<div key={todo.id} style={{ display: "flex" }}>
<div>{todo.id}</div>
<div>{todo.text}</div>
<div>{todo.done ? "Y" : "N"}</div>
</div>
))}
</div>
);
}
export default App;
๐ท react project ์ค์น
๐ท ํ๋ก ํธ๊ฐ ์๋ฒ์ ๋ฐ์ดํฐ ์์ฒญํ๋ ค๋ฉด "์๋ฒ์ฃผ์", "http ๋ฉ์๋" ํ์ => fetch ์ฌ์ฉ
๐ท ๋ฐ์ดํฐ ๊ฐ์ ธ์์ html ๋์ฐ๊ธฐ
์ฝ๋ ์ฐธ๊ณ ํ์ผ
https://github.com/gani0325/2023/tree/main/Web/Back_Server
728x90
๋ฐ์ํ
'๐ฉโ๐ป ๋ฐฑ์๋(Back-End) > Node js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Node js Project - 2] Store Locator API (0) | 2023.06.17 |
---|---|
[Node js Project - 1] Realtime Chat With Users & Rooms (0) | 2023.06.16 |
React (front) ์ Node (back) ์ฐ๋์ํค๊ธฐ (0) | 2023.05.18 |
[E-Commerce App with Fullstack] (8) Product API (1) | 2023.05.16 |
[E-Commerce App with Fullstack] (7) Category API (0) | 2023.05.15 |
Comments