Node.js 웹애플리케이션 글 삭제 기능 구현
삭제 수정은 반드시 POST방식을 써야함을 잊지말자. GET방식을 쓰면 취약점이 된다.
app_mysql.js
app.get('/topic/:id/delete', function(req, res){
var sql = 'SELECT id, title FROM topic';//글목록을 보여주기 위한 쿼리문
var id = req.params.id;
conn.query(sql, function(err, topics, fields){
var sql = 'SELECT * FROM topic WHERE id=?';
conn.query(sql, [id], function(err, topic){
if(err){//에러가 있다면 여기서 처리
res.status(500).send('Internal Server Error');
}else{
if(topic.length === 0){
console.log('There is no record.');
res.status(500).send('Internal Server Error');
}
// res.send(topic);//먼저 값을 보자
res.render('delete', {topics:topics, topic:topic[0]});
}
});
});
});
app.post('/topic/:id/delete', function(req, res){
var id = req.params.id;
var sql = 'DELETE FROM topic WHERE id=?';
conn.query(sql, [id], function(err, result){
res.redirect('/topic/');//삭제되면 초기화면으로감
});
});
view.jade 수정
doctype html
html
head
meta(charset='utf-8')
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 server side js.
ul
li
a(href='/topic/add') add
if topic
li
a(href='/topic/'+topic.id+'/edit') edit
li
a(href='/topic/'+topic.id+'/delete') delete
delete.jade
doctype html
html
head
meta(charset='utf-8')
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+'/delete' method='post')
p
input(type='submit' value='YES')
a(href='/topic/'+topic.id) no
실행 화면
delete가 추가됨.
delete 버튼을 누르면
다음과 같이 뜨고 no를 누르면
다시 돌아감
Yes를 누르면 삭제가 되고 첫 화면으로 돌아감
출처 : https://opentutorials.org/course/2136
'프로그래밍 > Node.js' 카테고리의 다른 글
Node.js 생활코딩 정리 – 세션으로 로그인 애플리케이션 만들기 (0) | 2016.12.01 |
---|---|
Node.js 생활코딩 정리 – Cookie 암호화 (0) | 2016.12.01 |
Node.js 생활코딩 정리 – Cookie를 이용한 장바구니 만들기 (0) | 2016.11.28 |
Node.js 생활코딩 정리 – HTTP 및 Cookie 구현하기 (0) | 2016.11.28 |
Node.js 생활코딩 정리 – 두번째 웹애플리케이션 글 편집 기능 구현 (0) | 2016.11.27 |
Node.js 생활코딩 정리 – 첫번째 MYSQL을 이용해서 웹애플리케이션 구현하기 (0) | 2016.11.27 |
Node.js 생활코딩 정리 – node-mysql 사용하기 (0) | 2016.11.27 |
Node.js 생활코딩 정리 – MYSQL 데이터베이스 사용법 (0) | 2016.11.27 |