๐Ÿ˜Ž ๊ณต๋ถ€ํ•˜๋Š” ์ง•์ง•์•ŒํŒŒ์นด๋Š” ์ฒ˜์Œ์ด์ง€?

ํ”„๋ก ํŠธ์—์„œ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์š”์ฒญํ•˜๊ธฐ (React๋กœ fetch, axios ์‚ฌ์šฉ) ๋ณธ๋ฌธ

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ฐฑ์—”๋“œ(Back-End)/Node js

ํ”„๋ก ํŠธ์—์„œ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์š”์ฒญํ•˜๊ธฐ (React๋กœ fetch, axios ์‚ฌ์šฉ)

์ง•์ง•์•ŒํŒŒ์นด 2023. 5. 18. 18:59
728x90
๋ฐ˜์‘ํ˜•

<๋ณธ ๋ธ”๋กœ๊ทธ๋Š” ๋ผ๋งค๊ฐœ๋ฐœ์ž ์˜ ์œ ํŠœ๋ธŒ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ๊ณต๋ถ€ํ•˜๋ฉฐ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค :-)>

=> ํ”„๋ก ํŠธ์—์„œ ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ ์š”์ฒญํ•˜๋Š” ๋ฐฉ๋ฒ• (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
๋ฐ˜์‘ํ˜•
Comments