c o d i n g . . ๐Ÿ‰/MySQL

[์ƒํ™œ์ฝ”๋”ฉ] Nodejs๋กœ Database ๋‹ค๋ฃจ๊ธฐ(3)

H J 2021. 7. 30. 04:14

์ด๋ฒˆ ์ˆ˜์—…์—์„œ๋Š” ์ €๋ฒˆ ์ˆ˜์—…๊นŒ์ง€ ์งฐ๋˜ ์ฝ”๋“œ์—์„œ fs๋ฅผ ์ด์šฉํ–ˆ๋˜ ๋ถ€๋ถ„์„ database๋ฅผ ์ด์šฉํ•˜๋„๋ก ๋ฐ”๊ฟ€ ๊ฒƒ์ด๋‹ค!

 

๊ทธ์ „์— app_file.js์˜ ๋ณต์‚ฌ๋ณธ app_mysql.js๋ฅผ ๋งŒ๋“ค์—ˆ๋Š”๋ฐ ์ €๋ฒˆ๊นŒ์ง€ ์งฐ๋˜ ์ฝ”๋“œ์™€ ๋‹ฌ๋ผ์„œใ…œใ…œ(multer ๋ถ€๋ถ„๋งŒ) ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด์คฌ๋‹ค

 

๋”๋ณด๊ธฐ

์ถ”๊ฐ€ํ•œ multer ๋ถ€๋ถ„

let multer = require('multer');

let _storage =multer.diskStorage({
    description: function (req, file, cb){
        cb(null, 'uploads/')
    },
    filename: function (req, file, cb){
        cb(null, file.originalname);
    }
})

let upload = multer({_storage: _storage})

 

๊ทธ๋ฆฌ๊ณ  database_mysql.js์—์„œ ๋งŒ๋“ค์—ˆ๋˜ ์ฝ”๋“œ ์ค‘ ์ผ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜จ๋‹ค!

let mysql      = require('mysql');
let conn = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '๋น„๋ฐ€๋ฒˆํ˜ธ',
  database : 'o2'
});

conn.connect();

 

topic/:id๋กœ ๋“ค์–ด๊ฐ”์„ ๋•Œ fs๊ฐ€ ์•„๋‹Œ DB๋ฅผ ์ด์šฉํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ค€๋‹ค

app.get(['/topic', '/topic/:id'], function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ
    let sql ='SELECT id, title FROM topic';
    conn.query(sql, function(err, rows, fields){
        res.send(rows);
    });
})

์ด๋ ‡๊ฒŒ sql๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ  ์—ฐ๊ฒฐ์˜ ์ฟผ๋ฆฌ๋กœ ์ด์šฉํ–ˆ์„ ๋•Œ id์™€ ์ œ๋ชฉ๋“ค์ด ์ œ๋Œ€๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

 

๋‹ค์Œ์œผ๋กœ ์ฟผ๋ฆฌ๋ฌธ ์•ˆ์— render๋ฅผ ์ด์šฉํ•ด์„œ view.pug๋กœ ์ •๋ณด๋ฅผ ๋ณด๋‚ด์ค€๋‹ค

์ด๋•Œ ์ €๋ฒˆ๊นŒ์ง€ ์ž‘์„ฑํ–ˆ๋˜ ์ฝ”๋“œ์™€ ์กฐ๊ธˆ ๋‹ค๋ฅด๊ฒŒ view.pug๋ฅผ ์ˆ˜์ •ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— view_mysql์ด๋ผ๋Š” ํด๋”๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ทธ ํด๋” ์•ˆ์˜ view.pug๋กœ ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด app.set ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์ค€๋‹ค

app.set('views', './views_mysql'); // ํ…œํ”Œ๋ฆฟ์—”์ง„์„ views_mysql์— ๋„ฃ๊ธฐ
app.get(['/topic', '/topic/:id'], function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ
    let sql ='SELECT id, title FROM topic';
    conn.query(sql, function(err, topics, fields){
        res.render('view', {topics:topics});
    });
})

app.get ๋ถ€๋ถ„์„ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค

 

์—ฌ๊ธฐ์„œ object๋กœ ๋œจ๋Š” ์ด์œ ๋Š” view,pug๋ฅผ ๋ณด์•˜์„ ๋•Œ topic์ด ์ œ๋Œ€๋กœ ์ „๋‹ฌ์ด ๋˜์ง€ ์•Š์•„์„œ์ด๊ณ !

js ํŒŒ์ผ์˜ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด rows๊ฐ€ ๊ฐ์ฒด๋กœ ์ „๋‹ฌ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค

 

doctype html
html
    head
        meta(charset="UTF-8")
        title Document
    body
        h1 
            a(href='/topic') Server Side JavaScript
        ul
            each topic in topics
                li
                    a(href="/topic/"+topic.id)= topic.title
        article
            h2= title
            = description
        div 
            a(href='/topic/new') new

 

view.pug ํŒŒ์ผ์˜ a(href="/topic/"+topic.id)= topic.title  ์ด ๋ถ€๋ถ„์—์„œ id, title์ด๋ผ๋Š” ๊ฒƒ์„ ๋ช…์‹œํ•ด์ฃผ๋ฉด ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ๋œจ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

๊ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํด๋ฆญํ•˜๋ฉด url์ด topic/id๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค! (์—ฌ๊ธฐ์„œ id ๊ฐ’์€ 1, 2, 3๊ณผ ๊ฐ™์€ ์‹๋ณ„์ž!)

 

๋‹ค์Œ์œผ๋กœ ๋กœ๊ณ ๋ฅผ ํด๋ฆญํ•˜๋ฉด ํ™˜์˜ ๋ฌธ๊ตฌ, ๊ฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํด๋ฆญํ•˜๋ฉด ์„ค๋ช…์ด ๋‚˜์˜ค๋„๋ก ์ฝ”๋“œ๋ฅผ ๊ณ ์น  ๊ฒƒ์ด๋‹ค

 

doctype html
html
    head
        meta(charset="UTF-8")
        title Document
    body
        h1 
            a(href='/topic') Server Side JavaScript
        ul
            each topic in topics
                li
                    a(href="/topic/"+topic.id)= topic.title
        article
            if topic 
                h2= title
                = description
            else 
                h2 Welcome 
                | This is tutorial.
        div 
            a(href='/topic/new') new

๋กœ๊ณ ๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ๋Š” topic์— ์•„๋ฌด๊ฒƒ๋„ ์ฃผ์ง€ ์•Š๊ณ  ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ์—๋Š” id ๊ฐ’์„ ์ฃผ์–ด์„œ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค

(view.pug์—์„œ topic์ด ์—†๋‹ค๋ฉด ํ™˜์˜๋ฌธ๊ตฌ๊ฐ€ ๋œจ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ–ˆ๋‹ค)

 

app.get(['/topic', '/topic/:id'], function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ
    let sql ='SELECT id, title FROM topic';
    conn.query(sql, function(err, topics, fields){
        let id = req.params.id; //id ๊ฐ€์ ธ์˜ค๊ธฐ
        if(id){ // id๊ฐ€ ์žˆ๋‹ค๋ฉด
            let sql ='SELECT * FROM topic WHERE id=?'; //์น˜ํ™˜์ž ์‚ฌ์šฉ
            conn.query(sql, [id], function(err, topics, fields){
                if(err){
                    console.log(err);
                }
                else{
                    res.render('view', {topics:topics, topic: topic[0]});
                }
            });
        }
        else{
            res.render('view', {topics:topics});
        }
        
    });
})

์ด๋•Œ ๋ฐ–์˜ topics๊ฐ€ ์•ˆ์˜ topics๋กœ ๊ฐฑ์‹ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•„๋ž˜์™€ ๊ฐ™์ด ๋œฐ ์ˆ˜ ์žˆ๋‹ค

์ด ๋•Œ ์•ˆ์˜ topics๋ฅผ topic์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•œ๋‹ค!

 

๋”๋ณด๊ธฐ
app.get(['/topic', '/topic/:id'], function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ
    let sql ='SELECT id, title FROM topic';
    conn.query(sql, function(err, topics, fields){
        let id = req.params.id; //id ๊ฐ€์ ธ์˜ค๊ธฐ
        if(id){ // id๊ฐ€ ์žˆ๋‹ค๋ฉด
            let sql ='SELECT * FROM topic WHERE id=?'; //์น˜ํ™˜์ž ์‚ฌ์šฉ
            conn.query(sql, [id], function(err, topic, fields){
                if(err){
                    console.log(err);
                }
                else{
                    res.render('view', {topics:topics, topic: topic[0]});
                }
            });
        }
        else{
            res.render('view', {topics:topics});
        }
        
    });
})

 

 

article
            if topic 
                h2= topic.title
                = topic.description

๊ทธ๋ฆฌ๊ณ  view.pug๋ฅผ ์ด๋ ‡๊ฒŒ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด topic/:id๋กœ ์ ‘๊ทผํ–ˆ์„ ๋•Œ ์„ค๋ช…์ด ์ œ๋Œ€๋กœ ๋ณด์ธ๋‹ค

 

๋”๋ณด๊ธฐ

์ €์ž๊นŒ์ง€ ์ถ”๊ฐ€!

doctype html
html
    head
        meta(charset="UTF-8")
        title Document
    body
        h1 
            a(href='/topic') Server Side JavaScript
        ul
            each topic in topics
                li
                    a(href="/topic/"+topic.id)= topic.title
        article
            if topic 
                h2= topic.title
                = topic.description
                div= 'by '+topic.author
            else 
                h2 Welcome 
                | This is tutorial.
        div 
            a(href='/topic/new') new

 

 

๊ธ€ ์ถ”๊ฐ€

 

๊ธ€์„ ์ถ”๊ฐ€ํ•ด์ค„ ๋•Œ new๊ฐ€ ์•„๋‹Œ add๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด views_mysql ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์˜ new.pug๋ฅผ add.pug๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค

 

app_mysql.js ๋˜ํ•œ ์ˆ˜์ •ํ•ด์ค€๋‹ค

app.get('/topic/add', function(req, res){ //form์„ topic/new๋กœ
    let sql ='SELECT id, title FROM topic';
    conn.query(sql, function(err, topics, fields){
        res.render('add', {topics:topics});
    })
})

 

add.pug์˜ topic์˜ ์ œ๋ชฉ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„์„ topic.title๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค

        ul 
            each topic in topics 
                li 
                    a(href="/topic/"+topic.id)= topic.title

 

view.pug์—์„œ๋„ new๋ฅผ add๋กœ, ์ฃผ์†Œ ๋˜ํ•œ /topic/new์—์„œ /topic/add๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค

div 
            a(href='/topic/add') add

 

์ƒˆ๋กœ์šด data๋ฅผ add ํ•  ๋•Œ post ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๊ธฐ์กด js ํŒŒ์ผ์˜ /topic ๋ถ€๋ถ„์„ /topic/add๋กœ ์ˆ˜์ •ํ•ด์ค€๋‹ค

app.post('/topic/add', function(req, res){ //post ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ
    let title = req.body.title;
    let description = req.body.description;
    let author = req.body.author;
    
    let sql='INSERT INTO topic (title, description, author) VALUES(?,?,?)';

    conn.query(sql, [title, description, author], function(err, rows, fields){
        if(err){
            console.log(err);
            res.status(500).send('Internal Server Error');
        }
        else{
            res.redirect('/topic/'+rows.insertId); // ๊ธ€ ์ž‘์„ฑ, ์ถ”๊ฐ€ ํ›„ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด
        }
    })
})

 

 

๊ธ€ ์ˆ˜์ • ๊ธฐ๋Šฅ

 

์ˆ˜์ • ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด view.pug ํŒŒ์ผ์— edit๋ฅผ list ํ˜•์‹์œผ๋กœ ์ถ”๊ฐ€์‹œ์ผœ์ค€๋‹ค

        ul 
            li 
                a(href='/topic/add') add 
            if topic 
                li 
                    a(href='/topic/'+topic.id+'/edit') edit

 

js ํŒŒ์ผ์— ์ˆ˜์ •์„ get ๋ฐฉ์‹์œผ๋กœ ๋ฐ›๋Š” ๋ผ์šฐํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค

app.get(['/topic/:id/edit'], function(req, res){ // ์ˆ˜์ •
    let sql ='SELECT id, title FROM topic';
    conn.query(sql, function(err, topics, fields){
        let id = req.params.id; //id ๊ฐ€์ ธ์˜ค๊ธฐ
        if(id){ // id๊ฐ€ ์žˆ๋‹ค๋ฉด
            let sql ='SELECT * FROM topic WHERE id=?'; //์น˜ํ™˜์ž ์‚ฌ์šฉ
            conn.query(sql, [id], function(err, topic, fields){ // ํ•ด๋‹น id์˜ ์ •๋ณด๋ฅผ ์ฝ์Œ
                if(err){
                    console.log(err);
                    res.status(500).send('Internal Server Error');
                }
                else{
                    res.render('edit', {topics:topics, topic: topic[0]}); // edit ์‚ฌ์šฉ
                }
            });
        }
        else{
            console.log('There isn"t id.');
            res.status(500).send('Internal Server Error');
        }
        
    });
})

 

์•ž์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์ •๋ณด๋ฅผ ์ฝ์–ด์˜ค๊ณ , ํ•ด๋‹น ์•„์ด๋””์˜ ์ •๋ณด๋ฅผ ์ฝ์–ด์„œ edit.pug ํŒŒ์ผ๋กœ ๋ณด๋‚ด์ค€๋‹ค

๊ธ€์„ ์ˆ˜์ •ํ•  ๋•Œ id๊ฐ€ ์—†๋‹ค๋ฉด ์˜ค๋ฅ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ž‘์„ฑํ•ด์ค€๋‹ค

 

edit.pug ์ƒ์„ฑ!

doctype html
html
    head
        meta(charset="UTF-8")
        title Document
    body
        h1 
            a(href='/topic') Server Side JavaScript
        ul
            each topic in topics
                li
                    a(href="/topic/"+topic.id)= topic.title
        article
            form(action="/topic/"+topic.id+"/edit" method="post")
                p 
                    input(type="text" name="title" placeholder="title" value=topic.title)
                p 
                    textarea(name="description" placeholder="description")
                        =topic.description
                p 
                    input(type="text" name="author" placeholder="author" value=topic.author)
                p 
                    input(type="submit")
        ul 
            li 
                a(href='/topic/add') add

edit.pug ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ฃผ๊ณ  edit ๋ฒ„ํŠผ์„ ์—†์• ๊ณ  data๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๋ถ€๋ถ„ ๋Œ€์‹  add.pug์˜ ๊ธ€ ์ž‘์„ฑ ๋ถ€๋ถ„์„ ๊ฐ€์ ธ์˜จ ํ›„ ํ•ด๋‹น id๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” data์˜ ์ •๋ณด๋ฅผ ๊ทธ๋Œ€๋กœ ๊ฐ€์ ธ์˜จ๋‹ค

 

 

์ด ๋ถ€๋ถ„๊นŒ์ง€ ํ–ˆ๋‹ค๋ฉด! ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•œ ํ›„ ์ œ์ถœ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด post ๋ฐฉ์‹์œผ๋กœ data๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ธ€ ์ˆ˜์ •์˜ post ๋ฐฉ์‹ ๋˜ํ•œ ๋ผ์šฐํŠธ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค

 

app.post('/topic/:id/edit', function(req, res){
    let title= req.body.title;
    let description= req.body.description;
    let author= req.body.author;
    let id=req.params.id;

    let sql= 'UPDATE topic SET title=?, description=?, author=? WHERE id=?';
    conn.query(sql, [title, description,author, id], function(err, rows, fields){
        if(err){
            console.log(err);
            res.status(500).send('Internal Server Error');
        }
        else{
            res.redirect('/topic/'+id);
        }
    })
})

๋ชจ๋“  data๋ฅผ ๊ฐ€์ ธ์˜จ ํ›„ update๋ฅผ ์‚ฌ์šฉํ•ด์„œ sql๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ  ์ œ์ถœ์„ ๋ˆ„๋ฅด๋ฉด ํ•ด๋‹น ์•„์ด๋””์˜ ์„ธ๋ถ€ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ํ•œ๋‹ค

 

 

์ˆ˜์ •ํ•˜๋Š” ์ค‘
์ˆ˜์ • ํ›„ ํ•ด๋‹น ์„ธ๋ถ€ ํŽ˜์ด์ง€๋กœ ์ด๋™

 

 

๊ธ€ ์‚ญ์ œ ๊ธฐ๋Šฅ

 

view.pug์— delete ๋ฒ„ํŠผ ์ƒ์„ฑ!

 

get ๋ฐฉ์‹์œผ๋กœ del ๋ผ์šฐํŠธ ํ•ด์ฃผ๊ธฐ!

app.get('/topic/:id/del', function(req, res){
    let sql ='SELECT id, title FROM topic';
    let id=req.params.id;
    conn.query(sql, function(err, topics, fields){
        let sql ='SELECT * FROM topic WHERE id=?';
        conn.query(sql, [id], function(err, rows, fields){
            if(err){
                console.log(err);
                res.status(500).send('Internal Server Error');
            }
            else{
                if(rows.length ===0){ // data๊ฐ€ ์—†๋‹ค๋ฉด
                    console.log('There isn"t record.');
                    res.status(500).send('Internal Server Error');
                }
                else{
                    res.render('delete', {topics: topics, topic: rows[0]});
                    // rows๋Š” ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— [0]์„ ๊ผญ ์จ์ค˜์•ผํ•จ
                }
                
            }
        })
        
    })
})

id๋ฅผ params๋ฅผ ์ด์šฉํ•ด ๊ฐ€์ ธ์˜จ ํ›„ ํ•ด๋‹น ์•„์ด๋””๋ฅผ ๊ฐ€์ง„ data๋ฅผ ์ฝ๊ณ  data๊ฐ€ ์—†๋‹ค๋ฉด ์˜ค๋ฅ˜์ฒ˜๋ฆฌ, ์žˆ๋‹ค๋ฉด delete.pug๋กœ ๋ Œ๋” ํ•ด์ค€๋‹ค

 

 

delete.pug ์ƒ์„ฑ!

doctype html
html
    head
        meta(charset="UTF-8")
        title Document
    body

        h1 
            a(href='/topic') Server Side JavaScript

        ul 
            each topic in topics 
                li 
                    a(href="/topic/"+topic.id)= topic.title
        
        article 
            h1= 'Delete? '+topic.title
            form(action='/topic/'+topic.id+'/del' method='post')
                p 
                    input(type='submit' value='YES')
            a(href='/topic/'+topic.id) NO

์‚ญ์ œ๋ฅผ ํ•  ๋• get ๋ฐฉ์‹์ด ์•„๋‹Œ post ๋ฐฉ์‹์œผ๋กœ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค

๋งŒ์•ฝ ๋ฏธ๋ฆฌ ๋งํฌ๋ฅผ ๋ฐฉ๋ฌธํ•˜์—ฌ ๋น ๋ฅด๊ฒŒ data๋ฅผ ์ œ๊ณตํ•ด์ฃผ๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ํ”Œ๋Ÿฌ๊ทธ์ธ์œผ๋กœ ์ธํ•ด data๊ฐ€ ๋ฐ”๋กœ ์‚ญ์ œ๋  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

 

์‚ญ์ œํ•œ๋‹ค๋ฉด post๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๊ณ  ์‚ญ์ œํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ•ด๋‹น id์˜ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ„๋‹ค

 

 

post ๋ฐฉ์‹์œผ๋กœ ๋ผ์šฐํŠธ ํ•ด์ฃผ๊ธฐ

 

app.post('/topic/:id/del', function(req, res){
    let sql ='DELETE FROM topic WHERE id=?';
    let id=req.params.id;
    conn.query(sql, [id], function(err, topics, fields){
       res.redirect('/topic/');        
    })
})

ํ•ด๋‹น id์˜ ๊ฐ’์„ ์‚ญ์ œํ•˜๊ณ  ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•œ๋‹ค

 

 

↓↓↓ ๊ทธ ๊ฒฐ๊ณผ

๋”๋ณด๊ธฐ
๊ธ€ ์ƒ์„ธ ํŽ˜์ด์ง€
delete๋ฅผ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ

NO๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋‹ค์‹œ ์ƒ์„ธ ํŽ˜์ด์ง€๋กœ ์ด๋™!

 

YES๋ฅผ ๋ˆ„๋ฅด๋ฉด! ๋ฆฌ์ŠคํŠธ์—์„œ ํ•ด๋‹น ๊ธ€์ด ์‚ญ์ œ๋˜๊ณ  ๋ฉ”์ธ ํŽ˜์ด์ง€๋กœ ์ด๋™!

 


ํœด์šฐ... ๋‹ค ๋“ค์—ˆ๋‹ค...

 

๋“ฃ๊ธฐ๋Š” ๋‹ค ๋“ค์—ˆ์œผ๋‹ˆ๊นŒ ๋‚ด์ผ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ •๋ฆฌํ•ด์•ผ๊ฒ ๋‹ค ;ใ……;

์˜ค๋Š˜์€ ๊ธ€์ด ์ข€ ๊ธธ์–ด์„œ ๋‚ด์ผ ์ •๋ฆฌํ•˜๋ฉด์„œ ๋‹ค์‹œ ์ˆ˜์ •ํ•˜๋Š” ๊ฑธ๋กœ!! 

 

์˜ค๋Š˜๋ถ€ํ„ฐ ๋‹ค์‹œ ๊ณต๋ถ€ ์‹œ์ž‘! ๐Ÿ’š๐Ÿ๐Ÿงค๐Ÿ”ซ๐Ÿฉฒ๐Ÿงƒโ˜˜