HyunJun 기술 블로그

Node.js HTTP Module, Express 본문

Express

Node.js HTTP Module, Express

공부 좋아 2023. 6. 21. 17:28
728x90
반응형

1. 서버 모듈

  • HTTP Module: 웹 서버를 만들기 위한, Node.js의 서버 모듈
  • Express: 웹 서버를 만들기 위한 Node.js의 웹 프레임워크, 서버 모듈, HTTP Module보다 더 간편, 많은 기능

2. 코드 비교

1) HTTP Module

// http module.
const http = require("http");
const httpPort = 3000;

// HTTP Module Server
const httpModuleServer = http.createServer((req, res) => {
  if (req.method === "GET" && req.url === "/http") {
    res.writeHead(200, {
      "Content-Type": "text/html",
    });
    res.end("http");
  }
});

// 실행
httpModuleServer.listen(httpPort, () => {
  console.log(`http server running at port ${httpPort}`);
});

2) Express Module (Framework)

// express web framework module.
const express = require("express");
const app = express();

const expressPort = 3000;

// Express Module Server
app.get("/express", function (req, res) {
  res.send("express");
});

// 실행
app.listen(expressPort, function () {
  console.log(`express running at ${expressPort}`);
});
기능이나 가독성, 편리함 등에서 Express가 깔끔함.
728x90
반응형
Comments