기본적인 서버 연결
const http = require("http");
const port = 3000;
const server = http.createServer((req, res) => {
if (req.url === "json") {
res.writeHead(200, {
"Contype-Type": "application/json",
});
res.end(JSON.stringify({ a: "a" }));
} else {
res.writeHead(200, {
"Contype-Type": "text/plain",
});
res.end("HELLO WORLD");
}
});
server.listen(port, () => {
console.log("서버 연결 완료");
});
CreateServer 메소드
HTTP server 객체
res.writeHead
응답 헤더를 객체로 전달하고 싶으면, 'Content-Type' : 'application/json'
응답 헤더를 텍스트로 전달하고 싶으면,
'Content-Type' : 'text/plain'
res.end
req.url