const http = require("http");
// 내부 모듈 가져오기
const server = http.createServer((rep,res)=>{
//req: http 요청의 정보, URL, 메소드 (GET, POST 등) 요청 헤더 정보, 바디의 내용이 있다.
//res: http 응답의 정보를 가지고 있는 객체 상태코드는 statusCode 응답 헤더, 바디의 내용이 있다.
res.statusCode = 201;
// 개발자도구에서 네트워크 탭 상태에서 확인할 수 있다.
res.setHeader("Content-Type", "application/json", "charset=utf-8");
//Content-Type : 응답의 내용
//application/json : 응답의 내용을 JSON 형식의 데이터로 전송
//charset=utf-8 : 응답의 문자를 인코딩 utf-8로 설정
//브라우저에서 요청한 url
const URL = req.url;
// 브라우저의 요청을 보내면 자동으로 웹사이트의 아이콘인 파비콘 URL이 자동으로 요청된다.
// 무시 처리를 해주자.
if(URL==="/favicon.ico"){
res.end();
return;
}
switch (URL) {
case "/": //localhost:4000/ 에 접속했을 때
res.end("main page");
break;
case "/list": //localhost:4000/list 에 접속했을 때
res.end("list page");
break;
case "/add": //localhost:4000/add 에 접속했을 때
res.end("add page");
break;
default:
break;
}
console.log("이게 요청한 URL"+ URL);
})