๊ด€๋ฆฌ ๋ฉ”๋‰ด

JiYoung Dev ๐Ÿ–ฅ

[Node.js] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™ํ•˜๊ธฐ (2023.07.06 ~ 07) ๋ณธ๋ฌธ

full stack/Back-End

[Node.js] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™ํ•˜๊ธฐ (2023.07.06 ~ 07)

Shinjio 2023. 7. 6. 13:50

DB์—ฐ๋™ํ•˜๊ธฐ

 

๋…ธ๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„ : nunjucks

html ์ƒ์—์„œ ๋ฐ˜๋ณต๋ฌธ, ์กฐ๊ฑด๋ฌธ์„ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ 

npm install nunjucks

 

nunjucks ์„ค์น˜ ํ›„ ๊ฒฝ๋กœ์™€ ํ˜•์‹ ์„ค์ •

 

nunjucks๋Š” ํ™•์žฅ์ž html ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ 

 

//nunjucks ํ™œ์šฉ์„ ์œ„ํ•œ ์„ค์ •
//html ๋ฌธ์„œ ๊ฒฝ๋กœ, ํ˜•์‹
//1. views ๊ฐ’ ์„ค์ • : view๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š” ํด๋”์˜ ๊ฒฝ๋กœ
app.set("views", __dirname + "/views");
//2.view engine : ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ํ˜•์‹
app.set("view engine", "html");
//nunjucks ์„ค์ •
nunjucks.configure("views", {
  express: app, //app(express) ๊ฐ์ฒด ์—ฐ๊ฒฐ
  watch: true, //html ํŒŒ์ผ์ด ์ œ๋Œ€๋กœ ์—ฐ๊ฒฐ๋˜๋ฉด ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ๋ Œ๋”๋งํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ
});

 

 

mySQL ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜

npm install mysql2

mysql vs mysql2 

mysql2 ๋Š” promises ํ˜•ํƒœ ->  ๋น„๋™๊ธฐ ๋ฐฉ์‹ ์ž‘์„ฑ์ด ์‰ฌ์›Œ์ง

mysql์€ promiseํ˜•ํƒœ๋ฅผ ์ง€์›ํ•˜๊ณ  ์žˆ์ง€ ์•Š์Œ 

 

db ์„ค์ • ์ •๋ณด ํŒŒ์ผ ์ž‘์„ฑ

 

//mySQL ์—ฐ๋™
//mysql2 : promises ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ
const mysql = require("mysql2");

const db_info = {
  host: "localhost",
  port: "3306",
  user: "",
  password: "",
  database: "boot",
};

//์„ค์ •๋“ค์„ app, router์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋‚ด๋ณด๋‚ด์•ผ ํ•จ
//์‹ค์ œ DB์™€ ์—ฐ๊ฒฐ -> ํ•จ์ˆ˜๋กœ ์ •์˜ -> ํ•จ์ˆ˜ ์ž์ฒด๋ฅผ ๋‚ด๋ณด๋‚ผ ์˜ˆ์ •
//์ดˆ๊ธฐํ™”, ์—ฐ๊ฒฐ -> 2๊ฐœ์˜ ํ•จ์ˆ˜ ์ž‘์„ฑ
module.exports = {
  init: function () {
    //db_info๋กœ ์—ฐ๊ฒฐ ๊ฐ์ฒด(connection) ์ƒ์„ฑ, ๋ฐ˜ํ™˜
    //mysql12์—์„œ ์—ฐ๊ฒฐ ๊ฐ์ฒด ์ƒ์„ฑ ๊ฐ€๋Šฅ
    return mysql.createConnection(db_info);
  },
  connect: function (conn) {
    //mysql ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ํ•จ์ˆ˜, conn -> ์—ฐ๊ฒฐ ๊ฐ์ฒด
    conn.connect(function (err) {
      if (err) console.log("์—ฐ๊ฒฐ ์‹คํŒจ !" + err);
      else console.log("์—ฐ๊ฒฐ ์„ฑ๊ณต !");
    });
  },
};

 

index.js(Router)์—์„œ ์‚ฌ์šฉ!

 

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

const conn = db.init(); //์—ฐ๊ฒฐ ๊ฐ์ฒด ์ƒ์„ฑ ํ›„ ๋ฐ˜ํ™˜
db.connect(conn); //์‹ค์ œ db ์—ฐ๊ฒฐ

//member ํ…Œ์ด๋ธ” ์ „์ฒด ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
router.get("/select", (req, res) => {
  //db์— ์žˆ๋Š” ๊ฐ’์€ ๊ณ„์† ๋‹ฌ๋ผ์ง€๊ณ  ๊ทธ ๊ฐ’์„ html์— ๋ฐ˜์˜ํ•ด์•ผ ํ•˜๋ฏ€๋กœ
  //sendFile ์‚ฌ์šฉ ๋ถˆ๊ฐ€
  //ํ™”๋ฉด์„ ๊ณ„์† ๋ Œ๋”๋งํ•ด์ค˜์•ผ ํ•จ -> render('html๊ฒฝ๋กœ',)

  //์‹คํ–‰ํ•œ sql๋ฌธ ์ •์˜
  let sql = "select * from member";
  //query(sql, ๊ฒฐ๊ณผ์ฒ˜๋ฆฌํ•จ์ˆ˜)
  //               err-์˜ค๋ฅ˜, rows-select๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ, fields - ๊ฒฐ๊ณผ ์™ธ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
  conn.query(sql, function (err, rows, fields) {
    // console.log(err);
    // console.log(rows);
    // console.log(fields);

    // ๊ฒฐ๊ณผ rows ๋ณด์—ฌ์ฃผ๊ธฐ
    //ํ…œํ”Œ๋ฆฟ ์—”์ง„ : html ์–‘์‹(ํ…œํ”Œ๋ฆฟ) + ๋ฐ์ดํ„ฐ -> ๊ฒฐ๊ณผ ๋ฌธ์„œ(nunjucks)
    //              -> ๊ฐ€์ง€๊ณ  ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•ด์„œ ํ™”๋ฉด ๋ Œ๋”๋ง
    res.render("index", {
      //key๊ฐ’ : ์‹ค์ œ value(๋ฐ์ดํ„ฐ)
      list: rows,
    });
  }); //sql๋ฌธ ์‹คํ–‰
});

module.exports = router;

 

redirect ํŠน์ง•

์„œ๋ฒ„ ์ฐจ์›์—์„œ ํŽ˜์ด์ง€ ์ด๋™ > forwarding > ์ฃผ์†Œ์ฐฝ์— ์ตœ์ข…์ ์œผ๋กœ ์–ด๋””๋กœ ์ด๋™ํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์—†์Œ 

์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋‹ค์‹œ ์š”์ฒญํ•˜๋„๋ก ๋ช…๋ น > ํด๋ผ์ด์–ธํŠธ๋Š” ์‹ค์ œ๋กœ ์š”์ฒญ์„ ํ•จ (์š”์ฒญ 2๋ฒˆ, ์‘๋‹ต 2๋ฒˆ)