9시 24분
글 생성, 수정, 삭제 기능 구현 본문
else if(pathname === '/create'){
db.query(`SELECT * FROM topic`, function(error, topics){
var title = 'Welcome';
var description = 'Hello, Node.js';
var list = template.list(topics);
var html = template.HTML(title, list,
`
<form action="/create_process" method="post">
<p><input type="text" name="title" placeholder="title"></p>
<p>
<textarea name="description" placeholder="description"></textarea>
</p>
<p>
<input type="submit">
</p>
</form>
`,
`<a href="/create">create</a>`
);
response.writeHead(200);
response.end(html);
});
}
글 생성 창에서 글 목록 띄우기 성공
이제 create_process 부분의 코드를 수정해서 글 생성 시 SQL에 추가, 글 목록에 추가, 작성한 글로 이동할 수 있도록 하자.
else if(pathname === '/create_process'){
var body = '';
request.on('data', function(data){
body = body + data;
});
request.on('end', function(){
var post = qs.parse(body);
var title = post.title;
var description = post.description;
db.query(`
INSERT INTO topic (title, description, created, author)
VALUES(?, ?, NOW(), ?)`,
[post.title, post.description, 'lee'],
function(error, result){
if(error) throw error;
response.writeHead(302, {Location: `/?id=${result.insertId}`});
response.end();
}
);
});
}
CRUD 중 C를 성공했다!
이제 U를 해보자!
얍
else if(pathname === '/update'){
db.query(`SELECT * FROM topic`, function(error, topics){
if(error) throw error;
db.query(`SELECT * FROM topic WHERE id=?`, [queryData.id], function(error2, topic){
var list = template.list(topics);
var html = template.HTML(topic[0].title, list,
`
<form action="/update_process" method="post">
<input type="hidden" name="id" value="${topic[0].id}">
<p><input type="text" name="title" placeholder="title" value="${topic[0].title}"></p>
<p>
<textarea name="description" placeholder="description">${topic[0].description}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>
`,
`<a href="/create">create</a> <a href="/update?id=${topic[0].id}">update</a>`
);
response.writeHead(200);
response.end(html);
});
});
}
글 수정 시 작성했던 내용이 뜨도록 하는데 성공했다.
이제 update_process 부분을 수정하여 글 수정 기능을 이어서 구현해보자.
else if(pathname === '/update_process'){
var body = '';
request.on('data', function(data){
body = body + data;
});
request.on('end', function(){
var post = qs.parse(body);
var id = post.id;
var title = post.title;
var description = post.description;
db.query(`UPDATE topic SET title=?, description=?, author=? WHERE id=?`,
[title, description, 'lee', id],
function(error, result){
response.writeHead(302, {Location: `/?id=${id}`});
response.end();
});
});
}
글 수정 기능도 구현 완료! 이제 마지막으로 D를 하러 가장
else if(pathname === '/delete_process'){
var body = '';
request.on('data', function(data){
body = body + data;
});
request.on('end', function(){
var post = qs.parse(body);
var id = post.id;
var filteredId = path.parse(id).base;
db.query(`DELETE FROM topic WHERE id=?`, [post.id], function(error, result){
if(error) throw error;
response.writeHead(302, {Location: `/`});
response.end();
});
});
}
아까 작성한 create update 글이 삭제되고 메인 화면으로 돌아오는 것을 볼 수 있다.
CRUD 완성 ~
'Javascript > Node.js' 카테고리의 다른 글
코드를 정리정돈 해보자 (0) | 2021.07.05 |
---|---|
저자 기능 추가 ( in 상세 글, 생성 ) (0) | 2021.07.05 |
MySQL 모듈 기본사용법, 홈페이지 구현, 상세보기 구현 (0) | 2021.07.05 |
실습 준비 (0) | 2021.07.05 |
callback, 패키지 매니저와 PM2, 글 생성, POST 방식으로 전송된 데이터 받기, 파일 생성과 리다이렉션 (0) | 2021.05.12 |