http 모듈

Minhyeok Kim·2022년 8월 30일
0

개념

목록 보기
4/13

http 모듈은 사용자와 직접적으로 커뮤니케이션을 만들어 낼 수 있는 공간으로 보인다. 직접 데이터를 입력받아서 짜놓은 프로그램안에있는 경로들을 돌면서 원하는 값을 보여주는 프로그램의 목적과 가장 가까이에 있는 부분 같다.

서버 오픈

const http = require("http");
// http 모듈 실행
const server = http.createServer((req, res) => {
  // 서버를 만들고
  if (req.url === "/") {
    // URL 이 "/" 으로 서버에 오게되면 
    res.write("welcome to our home page");
  }
  res.end();
});

server.listen(5000);
// 서버는 5000 번 포트에서 오픈
console.log("Welcome to Node Tutorial");

다른 URL 추가해보기

const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.end("welcome to our home page");
  }
  if (req.url === "/about") {
    res.end("here is our short history");
  }
});
// about 이라는 새로운 url 주소를 받는 코드를 작성
server.listen(5000);

console.log("Welcome to Node Tutorial");

HTTP methods

0개의 댓글