Framework/Express

Node.js / Express์—์„œ DB ์—ฐ๋™ํ•˜๊ธฐ

์„œ๋ฆฌโ˜… 2024. 7. 28. 18:43

๐Ÿš€ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ

 

๐Ÿ’พ Visual Studio Code

 

Visual Studio Code - Code Editing. Redefined

Visual Studio Code is a code editor redefined and optimized for building and debugging modern web and cloud applications.  Visual Studio Code is free and available on your favorite platform - Linux, macOS, and Windows.

code.visualstudio.com

 

๐Ÿ’พ MySQL Workbench

 

MySQL :: Download MySQL Workbench

Select Operating System: Select Operating System… Microsoft Windows Ubuntu Linux Red Hat Enterprise Linux / Oracle Linux Fedora macOS Source Code Select OS Version: All Windows (x86, 64-bit) Recommended Download: Other Downloads: Windows (x86, 64-bit), M

dev.mysql.com

 

๐Ÿ’พ Node.js

 

Node.js — Run JavaScript Everywhere

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.

nodejs.org

 

๐Ÿ’พ Express.js

Express.js ์„ค์น˜ ๋ช…๋ น์–ด
npm install express

 

๐Ÿ’พ MySQL2

MySQL2 ์„ค์น˜ ๋ช…๋ น์–ด
npm install mysql2

 

๐Ÿ’พ Nodemon

Nodemon ์„ค์น˜ ๋ช…๋ น์–ด
npm install nodemon

 

๐Ÿ’พ Nunjucks

Nunjucks ์„ค์น˜ ๋ช…๋ น์–ด
npm install nunjucks

 

 


 

1. MySQL Database ์„ค์ •

MySQL Workbench์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ ๋‹ค.

์˜ˆ์‹œ๋กœ 'nodejs' ์Šคํ‚ค๋งˆ์— member ํ…Œ์ด๋ธ”์„ ๋งŒ๋“ค์—ˆ๋‹ค.

CREATE TABLE `nodejs`.`member` (
  `id` VARCHAR(50) NULL,
  `pw` VARCHAR(50) NULL,
  `nick` VARCHAR(50) NULL);

 

 


 

2. Express.js ๊ธฐ๋ณธ ํด๋” ์ƒ์„ฑ

 

 

 ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ์•„๋ž˜์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํด๋”๋“ค์„ ์ƒ์„ฑํ•œ๋‹ค.

 

๐Ÿ“‚ config : ์„ค์ •์— ๊ด€๋ จ๋œ ์ •๋ณด๋“ค์„ ์ €์žฅ (DB ์—ฐ๊ฒฐ ์ •๋ณด, API KEY ๋“ฑ)

๐Ÿ“‚ public : ์ •์ ์ธ ํŒŒ์ผ๋“ค์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„ (CSS, JS, IMG, VIDEO ๋“ฑ)

๐Ÿ“‚ routes : ๊ฒฝ๋กœ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„

๐Ÿ“‚ views : ๋™์ ์ธ ์›นํŽ˜์ด์ง€๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ณต๊ฐ„

 

 


 

3. Node.js / Exress.js์™€ MySQL DB ์—ฐ๋™

 ์•„๋ž˜ ์˜ˆ์‹œ๋Š” ํšŒ์›๊ฐ€์ž…๊ณผ ๋กœ๊ทธ์ธ์„ํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

1) app.js

๐Ÿ“‚ project > ๐Ÿ“‘ app.js

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const nunjucks = require("nunjucks");
const mainRouter = require("./routes/mainRouter");
const userRouter = require("./routes/userRouter");

app.use(bodyParser.urlencoded({ extended: true }));

app.set("view engine", "html");
nunjucks.configure("views", {
  express: app,
  watch: true,
});

app.use("/", mainRouter);
app.use("/user", userRouter);

app.listen(3000);

 

2) db.js

DB์™€ ๊ด€๋ จ๋œ ์ •๋ณด๋Š” config ํด๋” ์•„๋ž˜์˜ db.js์— ์ž‘์„ฑํ•œ๋‹ค.

 

๐Ÿ“‚ config > ๐Ÿ“‘ db.js

// mysql2 ๋ชจ๋“ˆ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
const mysql = require("mysql2");

// DB ์—ฐ๊ฒฐ ์ •๋ณด
const conn = mysql.createConnection({
  host: "localhost",
  port: 3306,
  user: "root",
  password: "1234",
  database: "nodejs",
});

// ์—ฐ๊ฒฐ ์‹œ์ž‘
conn.connect();
console.log("DB ์—ฐ๊ฒฐ ์„ฑ๊ณต!");

module.exports = conn;

 

3) html

๐Ÿ“‚ views > ๐Ÿ“‘ main.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=<device-width>, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>ํšŒ์›๊ด€๋ฆฌ ์‹œ์Šคํ…œ</h1>
    <a href="join">ํšŒ์›๊ฐ€์ž…</a>
    <a href="login">๋กœ๊ทธ์ธ</a>
  </body>
</html>

 

๐Ÿ“‚ views > ๐Ÿ“‘ join.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€</h1>
    <form action="http://localhost:3000/user/join" method="post">
      id : <input type="text" name="id" /> <br />
      pw : <input type="password" name="pw" /> <br />
      nick : <input type="text" name="nick" /> <br />
      <input type="submit" />
    </form>
  </body>
</html>

 

๐Ÿ“‚ views > ๐Ÿ“‘ login.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</h1>
    <form action="http://localhost:3000/user/login" method="post">
      id : <input type="text" name="id" /> <br />
      pw : <input type="password" name="pw" /> <br />
      <input type="submit" />
    </form>
  </body>
</html>

 

4) router.js

๐Ÿ“‚ routes > ๐Ÿ“‘ mainRouter.js

const express = require("express");
const router = express.Router();

// 1. ๋ฉ”์ธ
router.get("/", (req, res) => {
  res.render("main");
});

// 2. ํšŒ์›๊ฐ€์ž…
router.get("/join", (req, res) => {
  res.render("join");
});

// 3. ๋กœ๊ทธ์ธ
router.get("/login", (req, res) => {
  res.render("login");
});

module.exports = router;

 

๐Ÿ“‚ routes > ๐Ÿ“‘ userRouter.js

const express = require("express");
const router = express.Router();
const conn = require("../config/db");

// 1. ํšŒ์›๊ฐ€์ž…
router.post("/join", (req, res) => {
  let { id, pw, nick } = req.body;
  
  // DB์™€ ์—ฐ๊ฒฐํ•ด์„œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
  // 1) ์ฒ˜๋ฆฌํ•  sql๋ฌธ ์ž‘์„ฑ
  // 2) ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ
  // 3) ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ œ์ž‘
  
  let sql = "insert into member values(?, ?, ?)";
  
  conn.query(sql, [id, pw, nick], (err, rows) => {
    console.log("ํšŒ์›๊ฐ€์ž… DB ๊ฒฐ๊ณผ : ", rows);
    
    // ๊ฒฐ๊ณผ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ
    if (rows) {
      res.redirect("/");
    }
  });
});

// 2. ๋กœ๊ทธ์ธ
router.post("/login", (req, res) => {
  let { id, pw } = req.body;
  
  // DB์™€ ์—ฐ๊ฒฐํ•ด์„œ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ
  // 1) ์ฒ˜๋ฆฌํ•  sql๋ฌธ ์ž‘์„ฑ
  // 2) ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ์— ๋งค๊ฐœ๋ณ€์ˆ˜์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ
  // 3) ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ œ์ž‘
  
  let sql = "select * from member where id = ? and pw = ?";
  conn.query(sql, [id, pw], (err, rows) => {
    console.log("๋กœ๊ทธ์ธ DB ๊ฒฐ๊ณผ : ", rows);
    
    // select๋ฌธ์„ ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์ 
    // 1) Data๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋•Œ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ œ๊ณต
    // 2) ์กฐํšŒ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋”๋ผ๋„ ๋น„์–ด์žˆ๋Š” ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    // 3) ์กฐ๊ฑด์„ ๋น„๊ตํ•  ๋•Œ ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ํ™•์ธํ•œ๋‹ค.
    // rows > 0๋กœ ๋น„๊ตํ•˜๋ฉด ์•ˆ๋˜๋Š” ์ด์œ  : ๋ฐฐ์—ด๊ณผ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๋ฉด ํ•ญ์ƒ false์ด๋‹ค.
    
    if (rows.length > 0) {
      console.log("๋กœ๊ทธ์ธ ์„ฑ๊ณต");
      res.redirect("/");
    } else {
      console.log("๋กœ๊ทธ์ธ ์‹คํŒจ");
    }
  });
});

module.exports = router;

 

 


 

4. ์š”์•ฝ ์ •๋ฆฌ

 

Express.js ์„ค์น˜ -> Express ๊ธฐ๋ณธ ํด๋” ์ƒ์„ฑ -> app.js ์ž‘์„ฑ -> db.js ์ž‘์„ฑ -> html ์ž‘์„ฑ -> router ์ž‘์„ฑ

 

* ์ถ”๊ฐ€๋กœ MySQL2์„ ์„ค์น˜ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ, Nodemon์„ ์„ค์น˜ํ•˜์—ฌ ์ž๋™ ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ๊ธฐ๋Šฅ ๊ตฌํ˜„, , Nunjucks๋ฅผ ์„ค์น˜ํ•˜์—ฌ ํ…œํ”Œ๋ฆฟ ์—”์ง„์œผ๋กœ ๋™์  HTML ํŽ˜์ด์ง€ ์ƒ์„ฑ ๋“ฑ ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ค์น˜ํ•˜์—ฌ ํ”„๋กœ์ ํŠธ์˜ ๊ธฐ๋Šฅ์„ฑ์„ ํ™•์žฅํ•˜๊ณ  ๊ฐœ๋ฐœ ํŽธ์˜์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.