[nodejs] 1. nodejs Getting start, 서버 구현하고 Hello world 띄어보기

2023. 11. 24. 20:40cs및 소프트스킬/nodejs

728x90
반응형
SMALL

이번 시간에는 nodejs를 직접 시작해보겠습니다.

nodejs를 "Hello world"를 띄우고, 구동원리의 대해 알아보겠습니다.

 

https://github.com/BerkleyLim/basic_nodejs

 

GitHub - BerkleyLim/basic_nodejs: 이 프로젝트는 node.js 입문을 위한 프로젝트 중 하나입니다.

이 프로젝트는 node.js 입문을 위한 프로젝트 중 하나입니다. Contribute to BerkleyLim/basic_nodejs development by creating an account on GitHub.

github.com

 

 

1. 소스코드

<ver01/app.js>

// http 라이브러리 연동
const http = require('http');

// 서버 주소, 포트 지정
const hostname = '127.0.0.1';
const port = 300;

// 서버 설정
const server = http.createServer((request, response) => {
  response.statusCode = 200;  // 상태 코드
  response.setHeader('Content-Type', 'text/plain'); // 헤더 설정
  response.end('Hello Worlds');  // 화면에 띄울 페이지
})

// 서버 실행
server.listen(port, hostname, () => {
  // 콘솔에 창을 띄어줍니다.
  console.log(`Server running at http://${hostname}:${port}`)
})

 

 

2. 실행

node ver01/app.js

 

 

3. 결과 

 

 

<코드 설명>

- app.js를 생성하여 node.js 서버를 만들어줍니다.
- 다음은 http를 임포트하여 지정하고, port, host IP 주소를 지정합니다.
- http 라이브러리를 이용하여 createServer를 이용하여 server 설정
- 이후, 서버 구동 실행

 

 

 

해당 코드는 Github 링크 타고, Ver01 을 참조하시기 바랍니다.

 

 

728x90
반응형
LIST