Server

ํ•™์Šต ํ‚ค์›Œ๋“œ

  • Express ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ Server ๊ตฌ์ถ•

  • Fetch API

  • Axios

Express ์ด์šฉํ•œ ๊ฐ„๋‹จํ•œ Server ๊ตฌ์ถ•

ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜

  • cors

  • express

  • nodemon

  • uuid

npm init -y

npm i express nodemon uuid

Server ๊ตฌ๋™ ๋ฐฉ๋ฒ•

  • ํ•ด๋‹น ๋ฐฉ์‹์€ ์ˆ˜์ •์‚ฌํ•ญ์„ ๋ฐ˜์˜ํ•ด์ฃผ์ง€ ์•Š์•„, server off ํ•˜๊ณ  ์žฌ๊ตฌ๋™

node server.js // ๐Ÿ‘ˆ๐Ÿป ๊ตฌ๋™ํ•  ํŒŒ์ผ
  • nodemon์€ server.js์˜ ์ˆ˜์ •์‚ฌํ•ญ์„ ๊ฐ์ง€ํ•ด์„œ ๋ฐ”๋กœ ๋ฐ˜์˜ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ

npx nodemon server.js

๐Ÿ“š API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•Œ์•„์•ผ ํ•˜๋Š” ๊ฐœ๋…

  • ์›น๋ธŒ๋ผ์šฐ์ €์™€ ์„œ๋ฒ„์˜ ํ†ต์‹  ํ•˜๊ธฐ์œ„ํ•ด์„œ HTTP ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉ

  • HTTP Protocol : TCP/IP ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์‚ฌ์ด์— ์ด๋ฃจ์–ด์ง€๋Š” ์š”์ฒญ/์‘๋‹ต ํ”„๋กœํ† ์ฝœ

  • TCP/IP ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ์‚ฌ์ด์— ์ด๋ฃจ์–ด์ง€๋Š” ์š”์ฒญ/์‘๋‹ต ํ”„๋กœํ† ์ฝœ

  • HTTP : TCP/IP

  • QUIC : UDP ๊ธฐ๋ฐ˜์œผ๋กœ ๋น ๋ฅธ ์ „์†ก ์†๋„๋ฅผ ๊ฐ€์ง€๋Š” ํ”„๋กœํ† ์ฝœ

  • HTTP Method : GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD, TRACE

    • GET : ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ โ†’ ๋ณด์•ˆ์˜ ์•ฝํ•จ

    • POST : ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ โ†’ ๋ณด์•ˆ์ƒ ์•ˆ์ •์„ฑ ๋†’์Œ

    • PUT : ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ โ†’ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์ฒด์ ์œผ๋กœ ์—…๋ฐ์ดํŠธ

    • PATCH : ๋ฐ์ดํ„ฐ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ โ†’ ๋ฐ์ดํ„ฐ๋ฅผ ์ผ๋ถ€๋ถ„๋งŒ ์—…๋ฐ์ดํŠธ

    • DELETE : ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ

  • Http/Https : Https(๋ณด์•ˆ ์ธ์ฆ์„œ๊ฐ€ ์ถ”๊ฐ€๋œ ํ”„๋กœํ† ์ฝœ) โ†’ ์ธํ”„๋ผ ์˜์—ญ

๐Ÿค” RESTful, REST ํ˜ธ์ถœํ•œ๋‹ค๋ผ๋Š” ์˜๋ฏธ๋Š”? HTTP Method๋ฅผ ๊ทœ์น™์— ๋งž๋Š” API ์„ค๊ณ„ ์ „๋žต

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป TodoList์˜ API ์ƒ์„ฑ

GET : Todos ์กฐํšŒ

let todos = [];

app.get('/todos', (req, res) => {
  res.status(200).json(todos);
});

POST : Todo ์ƒ์„ฑ

const bodyParser = require('body-parser');
const { v4: uuidv4 } = require('uuid');

app.post('/todos', (req, res) => {
  const { task } = req.body;
  const newTodo = {
    id: uuidv4(),
    task,
    completed: false,
  };
  res.status(200).json(newTodo);
});

PATCH : Todo Completed Update

app.patch('/todos/:id', (req, res) => {
  const { id } = req.params;
  const findTodo = todos.find((todo) => todo.id === id);

  if (!findTodo) {
    res.status(400).send('Not found Todo');
  }

  todos = todos.map((todo) =>
    todo.id === id ? { ...todo, completed: !todo.completed } : todo
  );

  res.status(200).send(findTodo);
});

PUT : Todo Task Update

app.put('/todos/:id', (req, res) => {
  const { id } = req.params;
  const { task } = req.body;
  const findTodo = todos.find((todo) => todo.id === id);

  if (!findTodo) {
    res.status(400).send('Not found Todo');
  }

  todos = todos.map((todo) => (todo.id === id ? { ...todo, task } : todo));

  res.status(200).send(todos.find((todo) => todo.id === id));
});

DELETE : ํ• ์ผ ์‚ญ์ œ

app.delete('/todos/:id', (req, res) => {
  const { id } = req.params;
  const findTodo = todos.find((todo) => todo.id === id);

  if (!findTodo) {
    res.status(400).send('Not found Todo');
  }

  todos = todos.filter((todo) => todo.id !== id);
  res.status(200).send('Todo Delete Success');
});

Fetch API

CORS

  • ํ•ด๊ฒฐํ•˜๋Š” ์ฃผ์ฒด๋Š” Backend (๊ทผ๋ณธ)

๐Ÿšจ Access to fetch at 'http://localhost:4000/todos' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

ํ•™์Šต์„ ๋ชฉ์ ์ž„์œผ๋กœ, Express ๋ฏธ๋“ค์›จ์–ด๋กœ ํ•ด๊ฒฐ

npm i cors
// server.js
const cors = require('cors');

app.use(cors());

GET

  • ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•

const fetchDate = () => {
  fetch('http://localhost:4000/todos') // ๐Ÿ‘ˆ๐Ÿป Promise ๋ฐ˜ํ™˜
    .then((res) => res.json())
    .then((data) => console.log(data))
    .catch((e) => console.log(e));
};
  • async/await ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

const fetchDateAsync = async () => {
  try {
    const response = await fetch('http://localhost:4000/todos');
    const data = await response.json();
    console.log(data);
  } catch (e) {
    console.log(e);
  }
};
const fetchDateAsync = async () => {
  try {
    const data = await (await fetch('http://localhost:4000/todos')).json();
    console.log(data);
  } catch (e) {
    console.log(e);
  }
};

POST

const fetchAdd = async () => {
  await fetch('http://localhost:4000/todos', {
    method: 'POST', // ๐Ÿ‘ˆ๐Ÿป ํ•ญ์ƒ ๋Œ€๋ฌธ์ž๋กœ
    headers: {
      'Content-Type': 'application/json', // ๐Ÿ‘ˆ๐Ÿป ๊ผญ Data์˜ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
    },
    body: JSON.stringify({ text: 'Learn React' }),
  });
};

PATCH

const fetchToggle = async () => {
  await fetch(`http://localhost:4000/todos/${id}`, {
    method: 'PATCH',
  });
};

DELETE

const fetchDelete = async () => {
  await fetch(`http://localhost:4000/todos/${id}`, {
    method: 'DELETE',
  });
};

Axios

  • ์„ค์น˜

npm i axios

๐Ÿง ๋ฉด์ ‘์งˆ๋ฌธ : ๋‘˜๋‹ค http ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š”๊ฑด ๋™์ผํ•˜๋‹ค. fetch API ๋‚ด์žฅ ํ•จ์ˆ˜์ด๊ณ , axios๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๊ธฐ ๋•Œ๋ฌธ์— fetch API ๋ณด๋‹ค ๋” ๋„“์€ ๋ฒ”์œ„๋ฅผ ์ปค๋ฒ„ ํ•  ์ˆ˜ ์žˆ๋‹ค. (์‚ฌ์šฉ์— ์žˆ์–ด ํŽธ์˜์„ฑ์„ ์ œ๊ณตํ•˜๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.)

GET

const axiosDate = () => {
  axios
    .get('http://localhost:4000/todos')
    .then((res) => {
      console.log(res);
      console.log(res.data);
    })
    .catch((e) => console.log(e));
};
const axiosDateAsync = async () => {
  try {
    const response = await axios.get('http://localhost:4000/todos');
    console.log(response.data);
  } catch (e) {
    console.log(e);
  }
};

POST

const axiosPost = async () => {
  try {
    await axios.post('http://localhost:4000/todos', {
      text: 'Learn React Axios',
    });
  } catch (e) {
    console.log(e);
  }
};

Patch

const axiosPatch = async () => {
  try {
    await axios.patch(`http://localhost:4000/todos/${id}`);
  } catch (e) {
    console.log(e);
  }
};

Delete

const axiosDelete = async () => {
  try {
    await axios.delete(`http://localhost:4000/todos/${id}`);
  } catch (e) {
    console.log(e);
  }
};

Last updated