์ด๋ฒ ์์ ์์๋ ์ ๋ฒ ์์ ๊น์ง ์งฐ๋ ์ฝ๋์์ 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์ด๋ผ๋ ๊ฒ์ ๋ช ์ํด์ฃผ๋ฉด ๋ฆฌ์คํธ๊ฐ ์ ๋๋ก ๋จ๋ ๊ฒ์ ๋ณผ ์ ์๋ค!

๋ค์์ผ๋ก ๋ก๊ณ ๋ฅผ ํด๋ฆญํ๋ฉด ํ์ ๋ฌธ๊ตฌ, ๊ฐ ๋ฆฌ์คํธ๋ฅผ ํด๋ฆญํ๋ฉด ์ค๋ช ์ด ๋์ค๋๋ก ์ฝ๋๋ฅผ ๊ณ ์น ๊ฒ์ด๋ค
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์ ๊ฐ์ ์ญ์ ํ๊ณ ๋ฉ์ธ ํ์ด์ง๋ก ์ด๋ํ๋ค
โโโ ๊ทธ ๊ฒฐ๊ณผ


NO๋ฅผ ๋๋ฅด๋ฉด ๋ค์ ์์ธ ํ์ด์ง๋ก ์ด๋!
YES๋ฅผ ๋๋ฅด๋ฉด! ๋ฆฌ์คํธ์์ ํด๋น ๊ธ์ด ์ญ์ ๋๊ณ ๋ฉ์ธ ํ์ด์ง๋ก ์ด๋!

ํด์ฐ... ๋ค ๋ค์๋ค...
๋ฃ๊ธฐ๋ ๋ค ๋ค์์ผ๋๊น ๋ด์ผ ๋ค์ ํ ๋ฒ ์ ๋ฆฌํด์ผ๊ฒ ๋ค ;ใ ;
์ค๋์ ๊ธ์ด ์ข ๊ธธ์ด์ ๋ด์ผ ์ ๋ฆฌํ๋ฉด์ ๋ค์ ์์ ํ๋ ๊ฑธ๋ก!!
์ค๋๋ถํฐ ๋ค์ ๊ณต๋ถ ์์! ๐๐๐งค๐ซ๐ฉฒ๐งโ

'c o d i n g . . ๐ > MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ํ์ฝ๋ฉ] ๊ด๊ณํ ๋ฐ์ดํฐ ๋ชจ๋ธ๋ง (0) | 2021.09.25 |
---|---|
[์ํ์ฝ๋ฉ] Nodejs๋ก Database ๋ค๋ฃจ๊ธฐ(2) (0) | 2021.07.20 |
[์ํ์ฝ๋ฉ] Nodejs๋ก Database ๋ค๋ฃจ๊ธฐ(1) (0) | 2021.07.19 |