메모

- express의 필요성을 느끼기 위해 http로 서버를 띄워본다.

- http는 내장 모듈이기 때문에 express처럼 npm으로 다운로드할 필요 없다.

- 코드를 보면 알겠지만, if else 문의 복잡함과 한글 인코딩 등을 처리해주어야 하기 때문에 express가 편하다는 걸 깨달을 수 있다.

코드

const http = require("http"); // 내장 모듈이기 떄문에 따로 다운 받을 필요 없음
const app = http.createServer((req, res) => {
    res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
    if (req.url === "/") {
        res.end("여기는 루트 입니다.");
    } else if (req.url === "/login") {
        res.end("여기는 로그인 화면입니다.");
    }
});

app.listen(3001, () => {
    console.log("server with http");
});


// const express = require("express"); // express 모듈 다운 받기
// const app = express(); // 실행시켜서 변수안에 넣기

// app.get("/", (req, res) => {
//     res.send("here is root.");
// });

// app.get("/login", (req, res) => {
//     res.send("here is login page.");
// })

// app.listen(3000, function () {
//     console.log("server on");
// });

 

참조

woorimIT - 백엔드 맛보기

squareyun