์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2ํŒ€] #6. http ๋ชจ๋“ˆ๋กœ ์„œ๋ฒ„ ๋งŒ๋“ค๊ธฐ & ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €

24-25/Node.js 2

by sksmsyena 2024. 11. 15. 10:00

๋ณธ๋ฌธ

728x90

๐ŸŒŸํ‚ค์›Œ๋“œ: ์š”์ฒญ๊ณผ ์‘๋‹ต, REST, ์ฟ ํ‚ค, ์„ธ์…˜, https, cluster, npm, package.json

 


1. ์š”์ฒญ๊ณผ ์‘๋‹ต

์„œ๋ฒ„์—๋Š” ์š”์ฒญ์„ ๋ฐ›๋Š” ๋ถ€๋ถ„๊ณผ ์‘๋‹ต์„ ๋ณด๋‚ด๋Š” ๋ถ€๋ถ„์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

์š”์ฒญ & ์‘๋‹ต์€ ์ด๋ฒคํŠธ ๋ฐฉ์‹์ด๋ฏ€๋กœ, ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋ฏธ๋ฆฌ ๋“ฑ๋กํ•ด๋‘ฌ์•ผํ•œ๋‹ค.

โ€‹

 

๋…ธ๋“œ ์„œ๋ฒ„๋ฅผ ๋งŒ๋“œ๋Š” ์ฝ”๋“œ

const http = require('http');

http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  res.write('<h1>Hello Node!</h1>');
  res.end('<p>Hello Server!</p>');
})
  .listen(8080, () => { // ์„œ๋ฒ„ ์—ฐ๊ฒฐ
    console.log('8080๋ฒˆ ํฌํŠธ์—์„œ ์„œ๋ฒ„ ๋Œ€๊ธฐ ์ค‘์ž…๋‹ˆ๋‹ค!');
  });

res ๊ฐ์ฒด์—๋Š” res.writeHead, res.write, res.end ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋‹ค.

→ res.writeHead๋Š” ์‘๋‹ต์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ๋Š” ์„ฑ๊ณต์ ์ธ ์š”์ฒญ์ž„์„ ์˜๋ฏธํ•˜๋Š” 200์„, ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ๋Š” ์‘๋‹ต์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณด๋‚ธ๋‹ค.

→ res.write์—๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

→ res.end๋Š” ์‘๋‹ต์„ ์ข…๋ฃŒํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

โ€‹

 

์›น ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์—ด์–ด http://localhost:8080์— ์ ‘์†ํ•˜๋ฉด, ์œ„์˜ ์„œ๋ฒ„๋กœ ์ด๋™ํ•œ๋‹ค.

์„œ๋ฒ„๋ฅผ ์ข…๋ฃŒํ•˜๋ ค๋ฉด ์ฝ˜์†”์—์„œ Ctrl+C๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

 

 

 

2. REST

*REST: ์„œ๋ฒ„์˜ ์ž์›์„ ์ •์˜ํ•˜๊ณ , ์ž์›์— ๋Œ€ํ•œ ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ์ฃผ์†Œ + HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

→ HTTP ์š”์ฒญ ๋ฉ”์†Œ๋“œ ์ค‘ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฉ”์†Œ๋“œ

 GET: ์„œ๋ฒ„ ์ž์›์„ ๊ฐ€์ ธ์˜ค๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์š”์ฒญ์˜ ๋ณธ๋ฌธ(body)์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์ง€ ์•Š๋Š”๋‹ค. ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์•ผ ํ•œ๋‹ค๋ฉด ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ์‚ฌ์šฉํ•œ๋‹ค.

 POST: ์„œ๋ฒ„์— ์ž์›์„ ์ƒˆ๋กœ ๋“ฑ๋กํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์š”์ฒญ์˜ ๋ณธ๋ฌธ์— ์ƒˆ๋กœ ๋“ฑ๋กํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ๋ณด๋‚ธ๋‹ค.

 PUT: ์„œ๋ฒ„์˜ ์ž์›์„ ์š”์ฒญ์— ๋“ค์–ด ์žˆ๋Š” ์ž์›์œผ๋กœ ์น˜ํ™˜ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์š”์ฒญ์˜ ๋ณธ๋ฌธ์— ์น˜ํ™˜ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ๋ณด๋‚ธ๋‹ค.

 PATCH: ์„œ๋ฒ„ ์ž์›์˜ ์ผ๋ถ€๋งŒ ์ˆ˜์ •ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์š”์ฒญ์˜ ๋ณธ๋ฌธ์— ์ผ๋ถ€ ์ˆ˜์ •ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด ๋ณด๋‚ธ๋‹ค.

 DELETE: ์„œ๋ฒ„์˜ ์ž์›์„ ์‚ญ์ œํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์š”์ฒญ์˜ ๋ณธ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์ง€ ์•Š๋Š”๋‹ค.

 OPTIONS: ์š”์ฒญ์„ ํ•˜๊ธฐ ์ „์— ํ†ต์‹  ์˜ต์…˜์„ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

- REST ์„œ๋ฒ„๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๋ฏ€๋กœ ์„œ๋ฒ„๋ฅผ ์ข…๋ฃŒํ•˜๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์†Œ์‹ค๋œ๋‹ค.

  '๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค'๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

โ€‹

3. ์ฟ ํ‚ค

- ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ฒ„๋Š” ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ํ•  ๋•Œ ์ฟ ํ‚ค๋ฅผ ๊ฐ™์ด ๋ณด๋‚ธ๋‹ค.

- ์ฟ ํ‚ค๋Š” ์œ ํšจ ๊ธฐ๊ฐ„์ด ์žˆ๋‹ค.

- name = zerocho์™€ ๊ฐ™์ด ๋‹จ์ˆœํ•œ 'ํ‚ค-๊ฐ’'์˜ ์Œ

- ์„œ๋ฒ„๋Š” ๋ฏธ๋ฆฌ ํด๋ผ์ด์–ธํŠธ์— ์š”์ฒญ์ž๋ฅผ ์ถ”์ •ํ•  ๋งŒํ•œ ์ •๋ณด๋ฅผ ์ฟ ํ‚ค๋กœ ๋งŒ๋“ค์–ด ๋ณด๋‚ด๊ณ , ๊ทธ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์ฟ ํ‚ค๋ฅผ ๋ฐ›์•„ ์š”์ฒญ์ž ํŒŒ์•…

 

์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค์–ด ์š”์ฒญ์ž์˜ ๋ธŒ๋ผ์šฐ์ €์— ๋„ฃ๋Š” ์ฝ”๋“œ

const http = require("http");

http.createServer((req, res) => {
    console.log(req.url, req.headers.cookie);
    res.writeHead(200, {'Set-Cookie' : 'mycookie=test'});
    res.end('Hello Cookie');
}).listen(8083, () => {
    console.log("8083๋ฒˆ ํฌํŠธ์—์„œ ์„œ๋ฒ„ ๋Œ€๊ธฐ ์ค‘์ž…๋‹ˆ๋‹ค!");
});

→ createServer ๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑ์—์„œ๋Š” req ๊ฐ์ฒด์— ๋‹ด๊ฒจ ์žˆ๋Š” ์ฟ ํ‚ค๋ฅผ ๊ฐ€์ ธ์˜จ๋‹ค.

(์ฟ ํ‚ค๋Š” req.headers.cookie์— ๋“ค์–ด์žˆ๋‹ค.)

→ res.writeHead ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์‘๋‹ต ํ—ค๋”์— ์ฟ ํ‚ค ๊ธฐ๋ก

→ Set-Cookie๋Š” ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋‹ค์Œ ๊ฐ’๊ณผ ๊ฐ™์€ ์ฟ ํ‚ค๋ฅผ ์ €์žฅํ•˜๋„๋ก ํ•œ๋‹ค.

→ ์‘๋‹ต์„ ๋ฐ›์€ ๋ธŒ๋ผ์šฐ์ €๋Š” mycookie=test๋ผ๋Š” ์ฟ ํ‚ค๋ฅผ ์ €์žฅ

 

 

โ€ป ๋ฏผ๊ฐํ•œ ๊ฐœ์ธ์ •๋ณด๋ฅผ ์ฟ ํ‚ค์— ๋„ฃ์–ด๋‘๋Š” ๊ฒƒ์€ ์ ์ ˆํ•˜์ง€X

์ฟ ํ‚ค์— ์ด๋ฆ„์„ ๋‹ด์•„์„œ ๋ณด๋‚ด๋Š” ๋Œ€์‹ , uniqueInt๋ผ๋Š” ์ˆซ์ž ๊ฐ’์„ ๋ณด๋‚ด๊ณ  ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„ & ๋งŒ๋ฃŒ ์‹œ๊ฐ„์€ uniqueInt ์†์„ฑ๋ช… ์•„๋ž˜์— ์žˆ๋Š” session์ด๋ผ๋Š” ๊ฐ์ฒด์— ๋Œ€์‹  ์ €์žฅํ•œ๋‹ค.

 

 

 

 

 

4. https์˜ ์•”ํ˜ธํ™”

- https ๋ชจ๋“ˆ์€ ์›น ์„œ๋ฒ„์— SSL ์•”ํ˜ธํ™”๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ GET์ด๋‚˜ POST ์š”์ฒญ์„ ํ•  ๋•Œ ์˜ค๊ฐ€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์•”ํ˜ธํ™”ํ•ด์„œ ์ค‘๊ฐ„์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์š”์ฒญ์„ ๊ฐ€๋กœ์ฑ„๋”๋ผ๋„ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์—†๊ฒŒ ํ•œ๋‹ค.

ex) ๋กœ๊ทธ์ธ, ๊ฒฐ์ œ๊ฐ€ ํ•„์š”ํ•œ ์ฐฝ์—์„œ https ์•”ํ˜ธํ™” ์ ์šฉ ํ•„์ˆ˜

 

 

 

 

โ€‹

5. cluster

*cluster ๋ชจ๋“ˆ: ์‹ฑ๊ธ€ ํ”„๋กœ์„ธ์Šค๋กœ ๋™์ž‘ํ•˜๋Š” ๋…ธ๋“œ๊ฐ€ CPU ์ฝ”์–ด๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ

- ํฌํŠธ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋‘˜ ์ˆ˜ ์žˆ์–ด, ์š”์ฒญ์ด ๋งŽ์ด ๋“ค์–ด์™”์„ ๋•Œ ์š”์ฒญ์„ ๋ถ„์‚ฐ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

→ ์„œ๋ฒ„์— ๋ฌด๋ฆฌ๊ฐ€ ๋œ ๊ฐ€๋Š” ํšจ๊ณผ

- ๋…ธ๋“œ๋Š” ๋ณดํ†ต ์ฝ”์–ด๋ฅผ ํ•˜๋‚˜๋งŒ ํ™œ์šฉํ•˜๋Š”๋ฐ, ํด๋Ÿฌ์Šคํ„ฐ๋ง์„ ํ•˜๋ฉด ์ฝ”์–ด๋ฅผ ํ•˜๋‚˜๋งŒ ํ™œ์šฉํ•  ๋•Œ์— ๋น„ํ•ด ์„ฑ๋Šฅ์ด ๊ฐœ์„ ๋œ๋‹ค.

- ํด๋Ÿฌ์Šคํ„ฐ๋ง์„ ์ ์šฉํ•ด๋‘๋ฉด ์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์—๋Ÿฌ๋กœ ์ธํ•ด ์„œ๋ฒ„๊ฐ€ ์ข…๋ฃŒ๋˜๋Š” ํ˜„์ƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

โ€‹

์œ„์˜ '์ฃผ์ œ1.์š”์ฒญ๊ณผ ์‘๋‹ต'์—์„œ ๋‚˜์™”๋˜ ๋…ธ๋“œ ์„œ๋ฒ„ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ๋งํ•œ ์ฝ”๋“œ

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
  console.log(`๋งˆ์Šคํ„ฐ ํ”„๋กœ์„ธ์Šค ์•„์ด๋””: ${process.pid}`);
  // CPU ๊ฐœ์ˆ˜๋งŒํผ ์›Œ์ปค๋ฅผ ์ƒ์‚ฐ
  for (let i = 0; i < numCPUs; i += 1) {
    cluster.fork();
  }
  // ์›Œ์ปค๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ
  cluster.on('exit', (worker, code, signal) => {
    console.log(`${worker.process.pid}๋ฒˆ ์›Œ์ปค๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.`);
    console.log('code', code, 'signal', signal);
  });
} else {
  // ์›Œ์ปค๋“ค์ด ํฌํŠธ์—์„œ ๋Œ€๊ธฐ
  http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
    res.write('<h1>Hello Node!</h1>');
    res.end('<p>Hello Cluster!</p>');
  }).listen(8086);

  console.log(`${process.pid}๋ฒˆ ์›Œ์ปค ์‹คํ–‰`);
}

 

 

 

 

 

6. npm

*npm = ๋…ธ๋“œ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €

- ๋Œ€๋ถ€๋ถ„์˜ jsํ”„๋กœ๊ทธ๋žจ์€ ํŒจํ‚ค์ง€ ์ด๋ฆ„์œผ๋กœ npm์— ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค.

- ํŠน์ • ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด npm์—์„œ ์ฐพ์•„ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค.

โ€‹

โ€‹

โ€‹

 

7. package.json

*package.json: ์„ค์น˜ํ•œ ํŒจํ‚ค์ง€์˜ ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•˜๋Š” ํŒŒ์ผ

โ˜…๋…ธ๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—๋Š” ํด๋” ๋‚ด๋ถ€์— ๋ฌด์กฐ๊ฑด package.json๋ถ€ํ„ฐ ๋งŒ๋“ค๊ณ  ์‹œ์ž‘ํ•ด์•ผ ํ•œ๋‹ค.

- ํŒจํ‚ค์ง€๋“ค์€ ์˜์กด ๊ด€๊ณ„๊ฐ€ ๋ณต์žกํ•˜๊ฒŒ ์–ฝํ˜€ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•˜๋‹ค.

- npm init ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜์—ฌ pacakge.json์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (ํด๋”๋ช…) [ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ์ž…๋ ฅ]
version: (1.0.0) [ํ”„๋กœ์ ํŠธ ๋ฒ„์ „ ์ž…๋ ฅ]
description: [ํ”„๋กœ์ ํŠธ ์„ค๋ช… ์ž…๋ ฅ]
entry point: index.js
test command: [์—”ํ„ฐ ํ‚ค ํด๋ฆญ]
git repository: [์—”ํ„ฐ ํ‚ค ํด๋ฆญ]
keywords: [์—”ํ„ฐ ํ‚ค ํด๋ฆญ]
author: [์—ฌ๋Ÿฌ๋ถ„์˜ ์ด๋ฆ„ ์ž…๋ ฅ]
license: (ISC) [์—”ํ„ฐ ํ‚ค ํด๋ฆญ]
About to write to C:\Users\zerocho/npmtest\package.json:

//...์ƒ๋žต

 

 

 

 


quiz

1. ์„œ๋ฒ„์˜ ์ž์›์„ ์ •์˜ํ•˜๊ณ  ์ž์›์— ๋Œ€ํ•œ ์ฃผ์†Œ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ (     ) ๋ผ๊ณ  ํ•œ๋‹ค.

โ€‹

2. ์‚ฌ์šฉ์ž๊ฐ€ ๋ˆ„๊ตฌ์ธ์ง€ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด ์„œ๋ฒ„๋Š” ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์„ ํ•  ๋•Œ (     )๋ฅผ ๊ฐ™์ด ๋ณด๋‚ธ๋‹ค.

โ€‹

3. ์ฟ ํ‚ค์— ๊ฐœ์ธ์ •๋ณด๋ฅผ ๋‹ด์•„์„œ ๋ณด๋‚ด๋Š” ๊ฒƒ์€ ๋ณด์•ˆ์ƒ ์ทจ์•ฝํ•˜๋ฏ€๋กœ, ์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„๊ณผ ๋งŒ๋ฃŒ ์‹œ๊ฐ„์€ ์ฟ ํ‚ค ๋Œ€์‹  (      ) ๊ฐ์ฒด์— ๋Œ€์‹  ์ €์žฅํ•œ๋‹ค.

โ€‹

4. https ๋ชจ๋“ˆ์€ (      ) ์•”ํ˜ธํ™”๋ฅผ ํ†ตํ•ด ์˜ค๊ฐ€๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘๊ฐ„์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๊ฐ€๋กœ์ฑ„๋”๋ผ๋„ ๊ทธ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์—†๊ฒŒ ํ•œ๋‹ค.

โ€‹

5. ํฌํŠธ๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ๋‘์–ด ์š”์ฒญ์„ ๋ถ„์‚ฐ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“ˆ์€ (      ) ์ด๋‹ค.

 

6. ํŠน์ • ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํŒจํ‚ค์ง€๊ฐ€ ํ•„์š”ํ•˜๋‹ค๋ฉด (      ) ์—์„œ ์ฐพ์•„ ์„ค์น˜ํ•˜๋ฉด ๋œ๋‹ค.

 

7. ์„ค์น˜ํ•œ ํŒจํ‚ค์ง€์˜ ๋ฒ„์ „์„ ๊ด€๋ฆฌํ•˜๋Š” ํŒŒ์ผ์€ (      ) ์ด๋‹ค.

 

programming quiz

8. http ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ HTTP ์„œ๋ฒ„๋ฅผ ์ƒ์„ฑํ•˜์‹œ์˜ค. ์„œ๋ฒ„๋Š” 8081๋ฒˆ ํฌํŠธ์—์„œ ์‹คํ–‰๋œ๋‹ค.

โ€‹

9. ์ฟ ํ‚ค๋ฅผ ๋งŒ๋“ค์–ด ๋ธŒ๋ผ์šฐ์ €์— ๋„ฃ๋Š” ์ฝ”๋“œ์ด๋‹ค. 1~4๋ฒˆ ์ฃผ์„์„ ์ฑ„์šฐ์‹œ์˜ค.

const http = require("http");

http.createServer((//1๋ฒˆ, res) => {
    console.log(//2๋ฒˆ, //3๋ฒˆ);
    res.writeHead(200, {//4๋ฒˆ : 'mycookie=test'});
    res.end('Hello Cookie');
}).listen(8083, () => {
    console.log("8083๋ฒˆ ํฌํŠธ์—์„œ ์„œ๋ฒ„ ๋Œ€๊ธฐ ์ค‘์ž…๋‹ˆ๋‹ค!");
});

 

 

 


Answer

1. REST

2. ์ฟ ํ‚ค

3. session

4. SSL

5. cluster

6. npm

7. package.json

8.

const http = require('http');

http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
  res.write('<h1>http ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ HTTP ์„œ๋ฒ„</h1>');
  res.end('<p>8081๋ฒˆ ํฌํŠธ์—์„œ ์‹คํ–‰๋˜๋Š” ์„œ๋ฒ„</p>');
})
  .listen(8081, () => {
    console.log('8081๋ฒˆ ํฌํŠธ์—์„œ ์„œ๋ฒ„ ๋Œ€๊ธฐ ์ค‘');
  });

 

9.

const http = require("http");

http.createServer((req, res) => {
    console.log(req.url, req.headers.cookie);
    res.writeHead(200, {'Set-Cookie' : 'mycookie=test'});
    res.end('Hello Cookie');
}).listen(8083, () => {
    console.log("8083๋ฒˆ ํฌํŠธ์—์„œ ์„œ๋ฒ„ ๋Œ€๊ธฐ ์ค‘์ž…๋‹ˆ๋‹ค!");
});

 

 

 

์ถœ์ฒ˜) ์กฐํ˜„์ •, ใ€ŒNode.js ๊ต๊ณผ์„œ ๊ฐœ์ • 3ํŒใ€, ๊ธธ๋ฒ—(2022), 4์žฅ-5์žฅ

 

Corner node.js 2ํŒ€

Editor : Pujimbao

728x90

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ