๐ŸŒณ๋ชฉํ‘œ

๋…ธ๋“œ์˜ ๊ธฐ๋ณธ ๋ชจ๋“ˆ ์ค‘ ๋„คํŠธ์› ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” http ๋ชจ๋“ˆ์„ ์•Œ์•„ ๋ด…๋‹ˆ๋‹ค. ํ—ฌ๋กœ์›”๋“œ ์ฝ”๋“œ๋กœ http๋ชจ๋“ˆ์„ ์ด์šฉํ•œ ์„œ๋ฒ„ ์–ดํ”Œ๋ ˆ์ผ€์ด์…˜์„ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ž…๋‹ˆ๋‹ค.

์™œ http ๋ชจ๋“ˆ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋‚˜์š”?

์šฐ๋ฆฌ๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด ๋ณผ๊ฒ๋‹ˆ๋‹ค. ์›น ์„œ๋ฒ„๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฝค ๋งŽ์€ ๋„คํŠธ์› ์ง€์‹์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ€๋ น tcp, listen, 3way handshaking ์ฒ˜๋Ÿผ ์ปดํ“จํ„ฐ๊ณตํ•™์—์„œ ๋ฐฐ์šฐ๋Š” ์ „๋ฌธ ์ง€์‹๋“ค์ด์ฃ .

๋…ธ๋“œ์—์„œ๋Š” ์ด๋Ÿฌํ•œ ๊ฐœ๋…๋“ค์„ ๊ตฌํ˜„ํ•œ http ๋ชจ๋“ˆ์„ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๋„คํŠธ์› ๊ธฐ์ˆ ์„ ๊ตฌํ˜„ํ•œ ๊ฒƒ์ด ์ด ๊ธฐ๋ณธ ๋ชจ๋“ˆ์ธ๋ฐ์š”, ์šฐ๋ฆฌ๋Š” ์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ ์ข€ ๋” ํŽธ๋ฆฌํ•˜๊ฒŒ ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์„œ๋ฅผ ๊ฐ™์ด ์‚ดํŽด ๋ณผ๊นŒ์š”?

๋…ธ๋“œ๋ฅผ ํ•™์Šตํ•˜๊ธฐ ์œ„ํ•œ ๊ต๊ณผ์„œ๋Š” ๋…ธ๋“œ ๊ณต์‹ ํ™ˆํŽ˜์ด์ง€์˜ ๋ฌธ์„œ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ๋ชจ๋“ˆ์ด ์–ด๋–ป๊ฒŒ ์žˆ๋‚˜ ํ•œ ๋ฒˆ ์‚ดํŽด ๋ณผ๊นŒ์š”?

๋…ธ๋“œ ๊ธฐ๋ณธ ๋ชจ๋“ˆ ๋ชฉ๋ก

์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋งŒ ์งš์–ด ๋ณผ๊ฒŒ์š”.

  • Console: ์ฝ˜์†” ๋กœ๊น…
  • Crypto: ์•”ํ˜ธํ™”
  • Events: ์ด๋ฒคํŠธ ๊ด€๋ฆฌ
  • File System: ํŒŒ์ผ ๊ด€๋ฆฌ
  • Http: ๋„คํŠธ์› ๊ธฐ๋Šฅ
  • OS: ์šด์˜์ฒด์ œ ์ •๋ณด
  • Path: ๊ฒฝ๋กœ ๊ด€๋ฆฌ
  • Stream: ์ŠคํŠธ๋ฆผ ๊ด€๋ฆฌ

์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  http ๋ชจ๋“ˆ์— ๋Œ€ํ•ด ๋” ์•Œ์•„ ๋ด…์‹œ๋‹ค.

http ๊ธฐ๋ณธ ๋ชจ๋“ˆ

  • http.createServer(): ์„œ๋ฒ„ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜
  • http.server: ์„œ๋ฒ„ ํด๋ž˜์Šค
  • server.listen(): ์„œ๋ฒ„๋ฅผ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ ๋Œ€๊ธฐ ์ƒํƒœ(listen)๋กœ ๋งŒ๋“ฌ

์œ„ ์„ธ ๊ฐœ API๋งŒ์œผ๋กœ ๊ฐ„๋‹จํ•œ ํ—ฌ๋กœ์›”๋“œ ์„œ๋ฒ„๋ฅผ ๋งŒ๋“ค์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”?

hello world

Hello word

๋งํฌ์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์—์„œ ์‹คํ–‰ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ์ง€๋‚œ ๊ธ€์—์„œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ๋ฏธ๋ฆฌ ์ €์žฅํ•ด ๋†“์€ ๋ธŒ๋žœ์น˜๋กœ ์ด๋™ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

$ git checkout -f scaffolding/unittest

์ฝ”๋“œ๋ฅผ ์ œ๋Œ€๋กœ ๋”ฐ๋ผ์˜ค์‹ ๋ถ„์€ ์ฒดํฌ์•„์›ƒ(๋ธŒ๋žœ์น˜ ์ด๋™)ํ•˜์ง€ ์•Š๊ณ  ๊ณ„์† ์ง„ํ–‰ํ•˜์‹œ๊ณ ์š”. ๊ทธ๋ ‡์ง€ ์•Š์€ ๋ถ„์€ ์ฒดํฌ์•„์›ƒ ํ•œ ๋’ค ์ด์–ด์„œ ์ฝ”๋”ฉํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

๋งํฌ์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์˜ server.js์— ๋ถ™์—ฌ ๋„ฃ์–ด ๋ด…์‹œ๋‹ค.

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'); // ์‘๋‹ต ํ—ค๋” ์ค‘ Content-Type ์„ค์ •
  res.end('Hello, World!\n'); // ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ „์†ก 
});

// ์„œ๋ฒ„๋ฅผ ์š”์ฒญ ๋Œ€๊ธฐ ์ƒํƒœ๋กœ ๋งŒ๋“ ๋‹ค 
server.listen(port, hostname, () => { // ์š”์ฒญ ๋Œ€๊ธฐ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ์‹คํ–‰๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜ 
  // ํ„ฐ๋ฏธ๋„์— ๋กœ๊ทธ๋ฅผ ๊ธฐ๋กํ•œ๋‹ค 
  console.log(`Server running at http://${hostname}:${port}/`);
});

์ถ”๊ฐ€๋กœ ๋‹ฌ์•„๋†“์€ ์ฃผ์„์„ ์ž์„ธํžˆ ์ฝ์–ด๋ณด์„ธ์š”. ์ถฉ๋ถ„ํžˆ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค.

์ €์žฅํ•˜๊ณ  ํ„ฐ๋ฏธ๋„์—์„œ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ด ๋ณด์ฃ .

$ node server.js
Server running at http://127.0.0.1:3000/

์ด๋Ÿฐ ๋ฉ”์„ธ์ง€๊ฐ€ ๋‚˜์˜ค์ฃ ? ์„œ๋ฒ„๊ฐ€ ๊ตฌ๋™๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. http.server ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด ์ ธ์„œ 127.0.0.1 ์ปดํ“จํ„ฐ(๋‚ด ์ปดํ“จํ„ฐ)์˜ 3000๋ฒˆ ํฌํŠธ์—์„œ ์š”์ฒญ ๋Œ€๊ธฐํ•˜๊ณ  ์žˆ๋Š” ์ƒํƒœ์ธ ๊ฒƒ์ด์ฃ .

๊ทธ๋Ÿผ curl ๋ช…๋ น์–ด๋กœ ์„œ๋ฒ„์— ์š”์ฒญํ•ด์„œ ์‘๋‹ต์„ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”?

$ curl localhost:3000 
Hello, world

โ€œHello, worldโ€ ๋ฌธ์ž์—ด์ด ์ œ๋Œ€๋กœ ์˜ค๋„ค์š”.

-v ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜๊ณ  curl ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด http ํ—ค๋”๊นŒ์ง€ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

curl localhost:3000 -v
* Rebuilt URL to: localhost:3000/
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Wed, 12 Dec 2018 10:48:33 GMT
< Connection: keep-alive
< Content-Length: 14
<
Hello, World!

> ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์š”์ฒญ ์ •๋ณด์ด๊ณ  < ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋ฉด ์‘๋‹ต ์ •๋ณด ์ž…๋‹ˆ๋‹ค. ์‘๋‹ต ๋ถ€๋ถ„์„ ๋ณด๋ฉด 200 ์ƒํƒœ์ฝ”๋“œ์™€ text/plain ํ—ค๋”๊ฐ€ ์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ •๋ฆฌ

  • ๋…ธ๋“œ ๊ธฐ๋ณธ ๋ชจ๋“ˆ์ธ http๋ฅผ ์‚ดํŽด ๋ดค์Šต๋‹ˆ๋‹ค.
  • http๋กœ ํ—ฌ๋กœ์›”๋“œ ์„œ๋ฒ„๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

๋ชฉ์ฐจ ๋ฐ”๋กœ๊ฐ€๊ธฐ