์๋ ํ์ธ์ Foma ์ ๋๋ค.
์ค๋์ Node.js์์ Express ํ๋ ์์ํฌ๋ฅผ ํ์ฉํ์ฌ MySQL ๋ฐ์ดํฐ ๋ฒ ์ด์ค์ ์์ฑ, ์กฐํ, ์์ , ์ญ์ (CRUD) ๋ฅผ ๊ตฌํํด ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
๋ฐ๋ก ์์ํ ๊ฒ์~
Tutorial
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 1 - ์๋ฒ ์ด๊ธฐ ์ธํ ํ๊ธฐ (Initialize server setting)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 2 - MySQL ํ ์ด๋ธ ๋ง๋ค๊ธฐ (Create a MySQL Table)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 3 - React Native ํ๋ฉด ๊ตฌํํ๊ธฐ (Implement frontend)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 4 - Express๋ก CRUD ๊ตฌํํ๊ธฐ (Building a CRUD API with Express)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 5 - Axios ์ด์ฉํ์ฌ CRUD ๊ตฌํํ๊ธฐ (Implementing a api with Axios)
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
Source code
์๋ ๋ ํฌ์งํ ๋ฆฌ๋ก ์ด๋ํ๋ฉด ์ ์ฒด ์์ค๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
๊ถ๊ธํ์ ์ ์์ผ์๋ฉด ์ธ์ ๋ ๋๊ธ ๋ฌ์์ฃผ์ธ์!
๋๊ธ