๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โš’ Backend/Node.js

[Node.js] React Native + Node.js + MySQL To do list ๋งŒ๋“ค๊ธฐ 4 - Express๋กœ CRUD ๊ตฌํ˜„ํ•˜๊ธฐ (Building a CRUD API with Express)

by Fomagran ๐Ÿ’ป 2022. 7. 27.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค.

 

์˜ค๋Š˜์€ Node.js์—์„œ Express ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ํ™œ์šฉํ•˜์—ฌ MySQL ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค์— ์ƒ์„ฑ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ (CRUD) ๋ฅผ ๊ตฌํ˜„ํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~


Tutorial


Install

 

HTTP ๋ณด์•ˆ ์ƒ์˜ ์ด์œ ๋กœ cors๋ฅผ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

(CORS๊ฐ€ ๋ญ”์ง€ ์ž์„ธํ•œ ๋‚ด์šฉ์ด ๊ถ๊ธˆํ•˜์‹œ๋ฉด ์—ฌ๊ธฐ ์—์„œ ํ™•์ธํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.)

 

npm install cors

Setting

 

ํ•„์š”ํ•œ ํ™˜๊ฒฝ์„ ๋ณ€์ˆ˜๋กœ ์„ธํŒ…ํ•ด ์ค๋‹ˆ๋‹ค.

 

const express = require("express");
const app = express();
const mysql = require("mysql");
const cors = require("cors");

 

express์—์„œ cors์™€ json์„ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

app.use(cors());
app.use(express.json());

 

MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์—ฐ๊ฒฐ์„ ์ƒ์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

const db = mysql.createConnection({
  user: "์œ ์ €์ด๋ฆ„",
  host: "๋กœ์ปฌํ˜ธ์ŠคํŠธ",
  password: "๋น„๋ฐ€๋ฒˆํ˜ธ",
  database: "๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์ด๋ฆ„",
});

 

express ์•ฑ์„ ์›ํ•˜๋Š” ํฌํŠธ์— ์‹คํ–‰์‹œ์ผœ ์ค๋‹ˆ๋‹ค.

 

app.listen(3001, () => {
  console.log("Your server is running on port 3001");
});

Building a API

 

Create

 

todo ์ •๋ณด๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์•„์™€ INSERT ์ฟผ๋ฆฌ๋ฅผ ํ†ตํ•ด MySQL ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

 

app.post("/create", (req, res) => {
  const author = req.body.author;
  const title = req.body.title;
  const content = req.body.content;
  const priority = req.body.priority;

  db.query(
    "INSERT INTO TODOLISTSYSTEM.TODOS (AUTHOR,TITLE,CONTENT,PRIORITY) VALUES (?,?,?,?)",
    [author, title, content, priority],
    (err, result) => {
      if (err) {
        console.log(err);
      } else {
        res.send("Inserted values successfully!");
      }
    }
  );
});

 

Read

 

TodoListSystem ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š” Todos ํ…Œ์ด๋ธ”์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ๋‘ ์ฝ์–ด์˜ต๋‹ˆ๋‹ค.

 

app.get("/todos", (req, res) => {
  db.query("SELECT * FROM TODOLISTSYSTEM.TODOS", (err, result) => {
    if (err) {
      console.log(err);
    } else {
      res.send(`Selected values successfully!`);
    }
  });
});

 

Update

 

id์— ํ•ด๋‹นํ•˜๋Š” Todo๋ฅผ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€๊ฒฝํ•ด ์ค๋‹ˆ๋‹ค.

 

app.put("/todos", (req, res) => {
  const todoid = req.body.todoid;
  const author = req.body.author;
  const title = req.body.title;
  const content = req.body.content;
  const priority = req.body.priority;

  db.query(
    "UPDATE TODOLISTSYSTEM.TODOS SET AUTHOR = ?, TITLE = ?, CONTENT = ?, PRIORITY = ? WHERE TODOID = ?;",
    [author, title, content, priority, todoid],
    (err, result) => {
      if (err) {
        console.log(err);
      } else {
        res.send(`Updated values successfully!`);
      }
    }
  );
});

 

Delete

 

์‚ญ์ œํ•  todoid๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€ ์‚ญ์ œ๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

app.delete("/todos/:todoid", (req, res) => {
  const { todoid } = req.params;
  db.query(
    "DELETE FROM TODOLISTSYSTEM.TODOS WHERE TODOID = ?",
    [todoid],
    (err, result) => {
      if (err) {
        console.log(err);
      } else {
        res.send(`Deleted values successfully!`);
      }
    }
  );
});

Next Tutorial

 

 

[React Native] React Native + Node.js + MySQL To do list ๋งŒ๋“ค๊ธฐ 5 - Axios ์ด์šฉํ•˜์—ฌ CRUD ๊ตฌํ˜„ํ•˜๊ธฐ (Implementing a api

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ๊ธ€์€ React Native + Node.js + MySQL ํŠœํ† ๋ฆฌ์–ผ์˜ ๋งˆ์ง€๋ง‰ ๋‹จ๊ณ„์ธ Axios๋ฅผ ํ™œ์šฉํ•˜์—ฌ CRUD๋ฅผ ๊ตฌํ˜„ํ•ด ์‹ค์ œ ์„œ๋ฒ„์— API๋ฅผ ์š”์ฒญํ•˜๊ณ  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๊ณ , ์ƒ์„ฑํ•˜๊ณ ,

fomaios.tistory.com


Source code

 

์•„๋ž˜ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋กœ ์ด๋™ํ•˜๋ฉด ์ „์ฒด ์†Œ์Šค๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ถ๊ธˆํ•˜์‹  ์  ์žˆ์œผ์‹œ๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”!

 

 

GitHub - fomagran/ToDoListFullStack: React native(CLI,TS) + Node.js + MySQL

React native(CLI,TS) + Node.js + MySQL. Contribute to fomagran/ToDoListFullStack development by creating an account on GitHub.

github.com

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€