본문 바로가기

Programming/JavaScript tips

[TIL] node.js 서버 구성

MAIN ACTIVITY :

Node.js 환경에서 서버를 만들고, 기존에 만들었던 채팅앱에서 GET 과 POST 를 써서 데이터를 주고 받을 수 있도록 만들었다.

아래 코드는 다른 모듈을 사용하지 않고 node.js만을 이용해 클라이언트의 요청에 따라 처리할 수 있도록 서버를 만든 것이다. 아주 간단한 어플리케이션 서버이고 이러한 구성으로 만들어 나갈 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const http = require('http');
 
const port = 3000;
const ip = '127.0.0.1';
 
const server = http.createServer((request, response) => {
  const statusCode = 200;
  const headers = { 'Content-Type''application/json' }
 
  if (request.method === 'GET' && request.url === '/') {
    response.writeHead(statusCode, headers);
    response.end(JSON.stringify(DATA));
  } else if (request.method === 'POST') {
    let body = '';
    request.on('data', chunk => {
      body += chunk;
    })
    request.on('end', () => {
      // body data handling here
      response.writeHead(statusCode, headers);
      response.end(JSON.stringify(DATA));
    })
  } else {
      response.writeHead(404, headers);
      response.end(ERROR);
  }
});
 
server.listen(port, ip);
cs

 

Learn :

아주 작은 개념의 서버를 구성했지만 http 의 사용, request, response handling, 그리고 요청에 따라 GET 과 POST 를 구분해 데이터를 받아오고 내려줄 수 있다. 그리고 server.listen() 을 통해서 어떤 포트와 아이피주소를 열어줄수 있다. 놓치면 안된다.

 

What Next :

Express 프레임워크를 사용하여 현재 작성된 node.js 서버를 리팩토링.

socket.io로 좀 더 원활한 채팅서버를 만들고 미들웨어와 라우터를 구성해 실제 채팅서버를 구현.

'Programming > JavaScript tips' 카테고리의 다른 글

Socket.io 다루기 - 실시간 투표앱, A to Z  (0) 2019.09.17
AWS 시대  (0) 2019.08.23
Node.js 서버사이드  (0) 2019.08.11
React.js_리액트  (0) 2019.08.05
setTimeout() && setInterval() 자바스크립트  (0) 2019.08.04