PATCH
1. PATCH
- 이번엔 PATCH 즉, 수정을 해봅시다.
- #1. 저번에 입력한 app.js에서 app.get(...) 밑에 다음과 같이 코드를 추가해줍니다. (저번 글 바로가기)
app.patch("/wise-sayings/:id", async (req, res) => {
// 요청을 보냄
const { id } = req.params;
const { author, content } = req.body;
const [rows] = await pool.query("SELECT * FROM wise_saying WHERE id = ?", [
id,
]);
if (rows.length == 0) {
res.status(404).send("not found");
return;
}
if (rows.author == 0) {
res.status(400).json({
msg: "author required",
});
return;
}
if (!content) {
res.status(400).json({
msg: "content required",
});
return;
}
const [rs] = await pool.query(
`
UPDATE wise_saying
SET content = ?,
author = ?
WHERE id = ?
`,
[content, author, id]
);
res.status(201).json({
id,
author,
content,
});
});
- #2. 그 다음, postman으로 들어가서 New Collection 옆에 점 3개를 누른 뒤, Add request를 클릭하여 추가해줍니다.
→ 그러면, 새로운 request가 생성되었습니다.
- #3. 그후 PATCH로 변경 후, 다음과 같이 입력해줍니다.
→ 또한, 수정하고 싶은 id를 고른 뒤, url에 http://localhost:3000/wise-sayings/(원하는 id)를 입력해줍니다.
{
"content" : "나는 즐겁다.",
"author" : "미상"
}
- #4. 다 되었으면 send를 클릭해봅시다!
→ 그러면 수정되었음을 확인할 수 있습니다.
- #5. 브라우저에 http://localhost:3000/wise-sayings 라고 입력하여 변경되었는지 확인해봅시다.
→ 제대로 변경되었습니다!
※ 전체 코드
import express from "express";
import mysql from "mysql2/promise";
// DB 설정
const pool = mysql.createPool({
host: "localhost",
user: "newbean",
password: "juv0312",
database: "wise_saying",
waitForConnections: true, // 연결하는 동안 대기 여부
connectionLimit: 10, // 연결 제한 개수
queueLimit: 0, // 최대 0(제한없음)개의 연결 요청을 대기열에 추가
});
const app = express();
app.use(express.json());
const port = 3000;
const wiseSayings = [
{
content: "나는 의적이다.",
author: "홍길동",
},
{
content: "나는 도적이다.",
author: "임꺽정",
},
];
app.get("/wise-sayings", async (req, res) => {
// 쿼리를 가져옴
const [rows] = await pool.query("SELECT * FROM wise_saying ORDER BY id DESC");
// json으로 담음
res.json(rows);
});
app.post("/wise-sayings", async (req, res) => {
const { author, content } = req.body;
if (!author) {
res.status(400).json({
msg: "author required",
});
return;
}
if (!content) {
res.status(400).json({
msg: "content required",
});
return;
}
const [rs] = await pool.query(
`
INSERT INTO wise_saying
SET regDate = NOW(),
content = ?,
author = ?
`,
[content, author]
);
// 성공 및 전송
res.status(201).json({
id: rs.insertId,
});
});
// 데이터를 원하는 id만 받아오기
app.get("/wise-sayings/:id", async (req, res) => {
// 요청을 보냄
const { id } = req.params;
const [rows] = await pool.query("SELECT * FROM wise_saying WHERE id = ?", [
id,
]);
// 없는 id를 요청하는 경우
if (rows.length == 0) {
res.status(404).send("not found");
return;
}
// json으로 담음s
res.json(rows[0]);
});
// 데이터 수정
app.patch("/wise-sayings/:id", async (req, res) => {
// 요청을 보냄
const { id } = req.params;
const { author, content } = req.body;
const [rows] = await pool.query("SELECT * FROM wise_saying WHERE id = ?", [
id,
]);
if (rows.length == 0) {
res.status(404).send("not found");
return;
}
if (rows.author == 0) {
res.status(400).json({
msg: "author required",
});
return;
}
if (!content) {
res.status(400).json({
msg: "content required",
});
return;
}
const [rs] = await pool.query(
`
UPDATE wise_saying
SET content = ?,
author = ?
WHERE id = ?
`,
[content, author, id]
);
res.status(201).json({
id,
author,
content,
});
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
'Back-End Study > Postman' 카테고리의 다른 글
DELETE (0) | 2023.12.01 |
---|---|
POST (0) | 2023.11.27 |
Postman (0) | 2023.11.24 |