const express = require("express");
const app = express();
const port = 3003;
// JSON 데이터 처리를 위해 필요
app.use(express.json());
// 임시 데이터베이스 역할을 하는 배열
let contacts = [
{ id: 1, name: "Alice", phone: "010-1234-5678" },
{ id: 2, name: "Bob", phone: "010-2345-6789" },
];
// 모든 연락처 가져오기
app.get("/contacts", (req, res) => {
res.status(200).json(contacts);
});
// 특정 연락처 상세보기
app.get("/contacts/:id", (req, res) => {
const contactId = parseInt(req.params.id);
const contact = contacts.find((c) => c.id === contactId);
if (contact) {
res.status(200).json(contact);
} else {
res.status(404).send("Contact not found");
}
});
// 새 연락처 추가하기
app.post("/contacts", (req, res) => {
const newContact = {
id: contacts.length + 1,
name: req.body.name,
phone: req.body.phone,
};
contacts.push(newContact);
res.status(201).json(newContact);
});
// 특정 연락처 수정하기
app.put("/contacts/:id", (req, res) => {
const contactId = parseInt(req.params.id);
const contact = contacts.find((c) => c.id === contactId);
if (contact) {
contact.name = req.body.name || contact.name;
contact.phone = req.body.phone || contact.phone;
res.status(200).json(contact);
} else {
res.status(404).send("Contact not found");
}
});
// 특정 연락처 삭제하기
app.delete("/contacts/:id", (req, res) => {
const contactId = parseInt(req.params.id);
const contactIndex = contacts.findIndex((c) => c.id === contactId);
if (contactIndex !== -1) {
const deletedContact = contacts.splice(contactIndex, 1);
res.status(200).json(deletedContact);
} else {
res.status(404).send("Contact not found");
}
});
app.listen(port, () => {
console.log(`${port} 포트번호 서버 실행중`);
});