[Node.js] MySQL로 기능 구현 (Update, Delete)
2023. 3. 27. 21:32ㆍ공부 중/Node.js
생활코딩 Node.js - MySQL 강의를 듣고서 작성한 글입니다. 그냥 그렇다고요.
1. Update
} else if (pathname === "/update") {
const filteredTitle = path.parse(queryData.id).base;
const description = fs.readFileSync(`${dataDir}/${filteredTitle}`, "utf8");
const updateForm = `
<form action="/update_process" method="post">
<p>
<input type="hidden" name="id" value="${filteredTitle}">
<input type="text" name="title" placeholder="title" value="${filteredTitle}"> </p>
<p>
<textarea name="description" placeholder="description">${description}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>
`;
let control = ``;
readAndRes(dataDir, filteredTitle, updateForm, control);
} else if (pathname === "/update_process") {
let body = "";
request.on("data", function (data) {
body += data;
});
request.on("end", function () {
let post = qs.parse(body);
let id = post.id;
let title = post.title;
let description = post.description;
let filteredId = path.parse(id).base;
let filteredTitle = path.parse(title).base;
let sanitizedId = sanitizeHtml(filteredId);
let sanitizedTitle = sanitizeHtml(filteredTitle);
let sanitizedDesc = sanitizeHtml(description);
fs.rename(
`${dataDir}/${sanitizedId}`,
`./data/${sanitizedTitle}`,
function (err) {
fs.writeFile(
`${dataDir}/${sanitizedTitle}`,
sanitizedDesc,
"utf8",
function (err) {
response.writeHead(302, {
Location: encodeURI(`/?id=${sanitizedTitle}`),
});
response.end();
}
);
}
);
});
가. else if (pathname === "/update")
} else if (pathname === "/update") {
db.query(
`SELECT * FROM topic WHERE id=?`,
[queryData.id],
function (error, topic) {
if (error) {
throw error;
}
const title = topic[0].title;
const description = topic[0].description;
const id = queryData.id;
let control = ``;
const updateForm = `
<form action="/update_process" method="post">
<p>
<input type="hidden" name="id" value="${id}">
<input type="text" name="title" placeholder="title" value="${title}"> </p>
<p>
<textarea name="description" placeholder="description">${description}</textarea>
</p>
<p>
<input type="submit">
</p>
</form>
`;
readAndRes(dataDir, title, updateForm, control);
}
);
db.query(
SELECT * FROM topic WHERE id=?, [queryData.id], function (error, topic) {…});
: 수정할 정보를 우선적으로 DB에서 가져온다.<input type="hidden" name="id" value="${id}">
: 또한 수정할 튜플의 id값도 넘겨준다.
나. else if (pathname === "/update_process")
} else if (pathname === "/update_process") {
let body = "";
request.on("data", function (data) {
body += data;
});
request.on("end", function () {
let post = qs.parse(body);
let id = post.id;
let title = post.title;
let description = post.description;
let sanitizedTitle = sanitizeHtml(title);
let sanitizedDesc = sanitizeHtml(description);
db.query(
`UPDATE topic
SET title=?, description=?, author_id=1
WHERE id=?`,
[sanitizedTitle, sanitizedDesc, id],
function (error, result) {
if (error) {
throw error;
}
response.writeHead(302, {
Location: encodeURI(`/?id=${id}`),
});
response.end();
}
);
});
db.query(
UPDATE topic SET title=?, description=?, author_id=1 WHERE id=?, [sanitizedTitle, sanitizedDesc, id], function (error, result) { … }
: UPDATE 쿼리문 작성Location: encodeURI(
/?id=${id})
: 리다이렉션 위치 수정
제출을 누르면
수정이 완료된다.
2. Delete
이제 하나 남았다.
before
} else if (pathname === "/delete_process") {
let body = "";
request.on("data", function (data) {
body += data;
});
request.on("end", function () {
let post = qs.parse(body);
let id = post.id;
let filteredId = path.parse(id).base;
fs.unlink(`${dataDir}/${filteredId}`, function (err) {
response.writeHead(302, { Location: `/` });
response.end();
});
});
after
} else if (pathname === "/delete_process") {
let body = "";
request.on("data", function (data) {
body += data;
});
request.on("end", function () {
let post = qs.parse(body);
let id = post.id;
db.query(`DELETE FROM topic WHERE id=?`,[id],function (error) {
if (error){
throw error;
}
response.writeHead(302, { Location: `/` });
response.end();
});
});
db.query(
DELETE FROM topic WHERE id=?,[id],function (result, error) { … }
: 삭제하는 쿼리
'공부 중 > Node.js' 카테고리의 다른 글
[Node.js] 리팩터링 (0) | 2023.03.27 |
---|---|
[Node.js] 작성자 표시 (0) | 2023.03.27 |
[Node.js] MySQL로 기능 구현 (Create, Read) (0) | 2023.03.27 |
[Node.js] npm install mysql2 (0) | 2023.03.13 |
[Node.js] MySQL 제어하기 (0) | 2023.03.10 |