관리 메뉴

정리왕

2. Node.js 클라이언트가 요청한 URL 알아보기 (공식홈 기준) 본문

6.개발/Node.js

2. Node.js 클라이언트가 요청한 URL 알아보기 (공식홈 기준)

정리합니다 2022. 3. 1. 21:23
반응형

Node.js에서 공식홈에서 알려준대로 서버를 만들어보고,

고객이 요청한 URL값을 어떻게 가져오는지 살펴 보겠습니다.

 

 


node.js 공식홈페이지에 방문하면,

Usage and example를 볼 수 있습니다.

 

https://nodejs.org/dist/latest-v17.x/docs/api/synopsis.html#usage

 

여기서 hello-world.js 를 살펴보면 이해할 수 있습니다.

 

 

 

1. 아래와 같이 코드를 칩니다.

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

 

 

2. cmd창에서 실행해 봅니다.

$ node hello-world.js

 

 

3. 콘솔 창에서 아래를 확인합니다.

Server running at http://127.0.0.1:3000/

 

URL을 확인할 수 있습니다.

 

4. 크롬창에서 주소를 입력 해 봅니다.

http://127.0.0.1:3000.

 

Hello, World!가 보입니다.

 

 

 

 

 

 

 


참고자료 : https://nodejs.org/dist/latest-v17.x/docs/api/synopsis.html

 

반응형

'6.개발 > Node.js' 카테고리의 다른 글

[Node.js] 주소 URL 요청하는 방법  (0) 2022.05.06
1. Node.js 서버 만들기  (0) 2022.03.01
Comments