server.js 파일 생헝 후 아래 코드 작성const http = require("http"); // Node.js 내장 모듈 불러오기
const server = http.createServer((req, res) => {
res.setHeader("Content-Type", "text/plain; charset=utf-8"); // 한글 지원 설정
res.end("안녕하세요, 오즈 코딩 스쿨!"); // 응답 전송
});
server.listen(3000, () => {
console.log("서버가 열렸어요! 포트: 3000");
});
node server.js

서버에서 Todo 데이터를 CRUD (Create, Read, Update, Delete) 할 수 있도록 기능을 구현
let todo = [
{ id: 1, content: "더미 데이터" },
{ id: 2, content: "더미 데이터 2" },
];
res.setHeader("Access-Control-Allow-Origin", "http://127.0.0.1:5500"); // 클라이언트 허용
res.setHeader("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); // 허용할 메소드 지정
if (req.method === "OPTIONS") {
return res.end("요청 보내세요.");
}
server.js에서 'GET' 요청 설정 (데이터 조회)if (req.method === "GET") {
return res.end(JSON.stringify(todo)); // JSON 형태로 데이터 반환
}
todo.js에서 readTodo와 renderDisplay를 구현const readTodo = async () => {
const res = await fetch("http://localhost:3000/");
const data = await res.json();
return data;
};
const readTodo = async () => {
const res = await fetch("http://localhost:3000/");
const data = await res.json();
return data;
};
데이터 가져오기 → POST 요청
1️⃣ req.on('data', callback)
data += chunk.toString(); 사용2️⃣ req.on('end', callback)
// POST 요청 처리
if (req.method === 'POST') {
let data
// chunk : 데이터가 조각 단위
// chunk는 버퍼(Buffer) 형태이므로, .toString()을 사용해 문자열로 변환
req.on('data', (chunk) => {
data = chunk.toString()
})
// 모든 데이터 조각(chunk)이 도착하면 "end" 이벤트가 발생
req.on('end', () => {
const newTodo = {id : Number(new Date()), content : data}
todo.push(newTodo)
})
return res.end('Todo가 추가되었습니다.')
}
todo.js에서 createTodo 및 createButton 클릭 이벤트 구현createTodoPOST, PUT, PATCH 등의 요청에서 사용되며, 서버에 새로운 데이터를 추가하거나 수정할 때 필요const createTodo = () => {
const newTodo = todoInput.value
//서버에 POST로 데이터 전달
fetch("http://localhost:3000/", {
method: "POST",
body:newTodo
})
.then(res => res.text())
.then(res => console.log(res))
};
createButton.addEventListener("click", createTodo);
createClick이벤트 코드click 이벤트에서 then 체인 사용
createTodo()가 실행되면 서버에 POST 요청을 보냄
.then(() => readTodo()) → createTodo() 완료 후 readTodo() 실행
.then((res) => {...}) → readTodo() 완료 후 화면을 업데이트
1️⃣ createTodo()가 끝나야 readTodo()를 실행
2️⃣ readTodo()가 끝나야 화면을 업데이트
3️⃣ 이 모든 과정이 비동기적으로 순서대로 실행됨
createButton.addEventListener("click", () => {
createTodo() // 서버 업데이트된 후에
.then(() => readTodo()) // 서버에서 todo 목록 가져오기
.then((res) => {
removeDisplay(); // 기존 화면 지우기
renderDisplay(res); // 새로운 목록 화면에 표시
});
});
server.js에서 PUT 요청 처리 // PUT 요청 처리
if (req.method === "PUT") {
let data;
req.on("data", (chunk) => {
data = chunk.toString();
});
req.on("end", () => {
const newTodo = JSON.parse(data)
todo = todo.map((el) => {
if (el.id === newTodo.id) {
return newTodo
} else {
return el
}
})
});
return res.end("Todo가 수정되었습니다.");
}
todo.js에서 개별 todo에 대한 내용 수정 및 삭제를 위한 요소 생성const renderDisplay = (data) => {
for (let el of data) {
const list = document.createElement("li");
const todoContent = document.createElement('span');
todoContent.textContent = el.content;
// 업데이트를 위한 input과 삭제 버튼 생성 (개별 리스트마다 필요함)
const updateInput = document.createElement('input')
const updateButton = document.createElement('button')
updateButton.textContent = '수정'
const deleteButton = document.createElement('button')
deleteButton.textContent = '삭제'
list.append(todoContent, updateInput, updateButton, deleteButton)
todoList.append(list);
}
};
const updateTodo = (newTodo) => {
const newTodo = todoInput.value;
return fetch("http://localhost:3000/", {
method: "PUT", // 수정을 위해 PUT 메소드 사용
body: newTodo,
})
.then((res) => res.text()) // 서버 응답을 text로 변환
.then((res) => console.log(res)); // 변환된 응답을 콘솔에 출력
};
const updateInput = document.createElement('input')
const updateButton = document.createElement('button')
updateButton.textContent = '수정'
updateButton.onclick=() => {
updateTodo({
id : el.id,
content : updateInput.value
})
}
// 업데이트를 위한 input과 삭제 버튼 생성 (개별 리스트마다 필요함)
const updateInput = document.createElement('input')
const updateButton = document.createElement('button')
updateButton.textContent = '수정'
updateButton.onclick=() => {
updateTodo({
id : el.id,
content : updateInput.value
})
.then(() => readTodo())
.then((res) => {
removeDisplay()
renderDisplay(res)
})
}
server.js에서 DELETE 요청 처리 // DELETE 요청 처리
if (req.method === "DELETE") {
let data;
req.on("data", (chunk) => {
data = chunk.toString();
});
req.on("end", () => {
const id = Number(data); // 문자열로 들어오는 id 데이터를 숫자로
todo = todo.filter((el) => el.id !== id);
});
return res.end("Todo가 삭제되었습니다.");
}
todo.js에서 deleteTodo 메소드 생성 및 deleteButton 클릭 이벤트 관리// 받아온 ID와 비교해서 삭제 실행
const deleteTodo = (id) => {
return fetch("http://localhost:3000/", {
method: "DELETE", // 삭제를 위해 DELETE 메소드 사용
body: id,
})
.then((res) => res.text()) // 서버 응답을 text로 변환
.then((res) => console.log(res)); // 변환된 응답을 콘솔에 출력
};
const deleteButton = document.createElement("button");
deleteButton.textContent = "삭제";
deleteButton.onclick = () => {
deleteTodo(el.id)
}
const deleteButton = document.createElement("button");
deleteButton.textContent = "삭제";
deleteButton.onclick = () => {
deleteTodo(el.id)
.then(() => readTodo())
.then((res) => {
removeDisplay();
renderDisplay(res);
});
};

1️⃣ Node.js의 HTTP 모듈을 사용하여 서버를 구축할 수 있다.
2️⃣ 서버는 클라이언트의 요청을 받고, 응답을 반환하는 방식으로 통신한다.
3️⃣ 클라이언트와 서버의 출처(Origin)가 다를 경우, CORS 설정을 통해 요청을 허용해야 한다.
4️⃣ 서버는 요청(Method: GET, POST, PUT, DELETE 등)에 따라 적절한 처리를 해야 하며, 이를 위해 조건문을 활용하여 분기 처리할 수 있다.
5️⃣ 특정 포트(port)를 통해 요청을 수락하고, 해당 포트에서 서버가 실행될 수 있다.