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

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2ํŒ€] #4.๋…ธ๋“œ ๊ธฐ๋Šฅ ์•Œ์•„๋ณด๊ธฐ(1)

24-25/Node.js 2

by sksmsyena 2024. 10. 11. 12:28

๋ณธ๋ฌธ

728x90

๐ŸŒŸํ‚ค์›Œ๋“œ: REPL, CommonJS ๋ชจ๋“ˆ, ECMAScript ๋ชจ๋“ˆ, ๋‚ด์žฅ ๊ฐ์ฒด, ๋‚ด์žฅ ๋ชจ๋“ˆ


1. REPL 

๋…ธ๋“œ์—์„œ ์ œ๊ณตํ•˜๋Š” ์ฝ˜์†”๋กœ, ์ž…๋ ฅํ•œ ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ (Read), ํ•ด์„ํ•˜๊ณ (Eval), ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ (Print), ์ข…๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต(Loop)ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.

 

-REPL ์‚ฌ์šฉ๋ฐฉ๋ฒ•

  ํ„ฐ๋ฏธ๋„์— node๋ฅผ ์ž…๋ ฅํ•œ๋‹ค, ํ”„๋กฌํ”„ํŠธ๊ฐ€ >๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค๋ฉด ์ž…๋ ฅ ๊ฐ€๋Šฅํ•œ ์ƒํƒœ์ด๋‹ค.

-REPL ์ข…๋ฃŒ๋ฐฉ๋ฒ•

  Ctrl+C๋ฅผ ๋‘ ๋ฒˆ ๋ˆ„๋ฅด๊ฑฐ๋‚˜, ์ฐฝ์— .exit๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.


2. JS ํŒŒ์ผ ์‹คํ–‰

์ฝ˜์†”์— node [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ]

 ํŒŒ์ผ์„ ์ €์žฅํ•  ๋•Œ๋Š” .js๋กœ ์ €์žฅํ•˜๊ณ , ์‹คํ–‰์‹œ ํ™•์žฅ์ž(.js)๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜๋‹ค.

 

โ€ป REPL์ด ์•„๋‹ˆ๋ผ ์ฝ˜์†”์—์„œ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค. 

โ€ป ํŒŒ์ผ์ด๋‚˜ ํด๋”์˜ ์ด๋ฆ„์— /, \, |, <, >, :, ", ?, * ๋“ฑ์€ ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


3. ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค๊ธฐ

๋ชจ๋“ˆ์ด๋ž€ ํŠน์ •ํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ์ด๋‹ค.

๋…ธ๋“œ๋Š” ํŒŒ์ผ๋ณ„๋กœ ์ฝ”๋“œ๋ฅผ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค์–ด ํ•ด๋‹น ๋ชจ๋“ˆ์„ ์žฌ์‚ฌ์šฉํ•˜๊ณ , ๊ด€๋ฆฌ๋„ ์šฉ์ดํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. 

 

๋ชจ๋“ˆ๊ณผ ํ”„๋กœ๊ทธ๋žจ


3-1. CommonJS ๋ชจ๋“ˆ

ํ‘œ์ค€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ์€ ์•„๋‹ˆ์ง€๋งŒ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋„๋ฆฌ ์“ฐ์ด๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

require()๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

 

+require()

-require()๋Š” ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ•จ์ˆ˜์ด๋‹ค. 

-๋ฐ˜๋“œ์‹œ ํŒŒ์ผ์˜ ์ตœ์ƒ๋‹จ์— ์œ„์น˜ํ•  ํ•„์š”์—†๋‹ค.

-require.cache ๊ฐ์ฒด์— ํŒŒ์ผ ์ด๋ฆ„์ด ์†์„ฑ๋ช…์œผ๋กœ ๋“ค์–ด ์žˆ๋‹ค. ์†์„ฑ๊ฐ’์œผ๋กœ๋Š” ๊ฐ ํŒŒ์ผ์˜ ๋ชจ๋“ˆ ๊ฐ์ฒด๊ฐ€ ์žˆ๋‹ค.

-require.main์€ ๋…ธ๋“œ ์‹คํ–‰ ์‹œ ์ฒซ ๋ชจ๋“ˆ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

 

//var.js
const odd = 'CJS ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค';
const even = 'CJS ์ง์ˆ˜์ž…๋‹ˆ๋‹ค';

module.exports = {
  odd,
  even,
};

 module.exports๋กœ ํ•œ ๋ฒˆ์— ๋Œ€์ž…ํ•˜๋Š” ๋Œ€์‹ , ๊ฐ๊ฐ์˜ ๋ณ€์ˆ˜๋ฅผ exports ๊ฐ์ฒด์— ํ•˜๋‚˜์”ฉ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค(module.exports==exports).

//func.js
const { odd, even } = require('./var');

function checkOddOrEven(num) {
  if (num % 2) { // ํ™€์ˆ˜๋ฉด
    return odd;
  }
  return even;
}

module.exports = checkOddOrEven;
//index.js
const { odd, even } = require('./var');
const checkNumber = require('./func');

function checkStringOddOrEven(str) {
  if (str.length % 2) { // ํ™€์ˆ˜๋ฉด
    return odd;
  }
  return even;
}

console.log(checkNumber(10));
console.log(checkStringOddOrEven('hello'));

 

-๋ชจ๋“ˆ ํ•˜๋‚˜๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ , ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. (var.js๊ฐ€ func.js์™€ index.js์— ๋‘ ๋ฒˆ ์“ฐ์ธ๋‹ค)

-์—ฌ๋Ÿฌ ํŒŒ์ผ์— ์žฌ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค์–ด๋‘๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค.

 

+๋…ธ๋“œ์—์„œ this

-์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„(์‹๋ณ„์ž๊ฐ€ ์œ ํšจํ•œ ๋ฒ”์œ„)์—์„œ this๋Š” module.exports๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

-ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ ๋‚ด๋ถ€์˜ this๋Š” global ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.


 

3-2. ECMAScript ๋ชจ๋“ˆ

๊ณต์‹์ ์ธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ํ˜•์‹์ด๋‹ค.

//var.mjs
export const odd = 'MJS ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค';
export const even = 'MJS ์ง์ˆ˜์ž…๋‹ˆ๋‹ค';
//func.mjs
import { odd, even } from './var.mjs';

function checkOddOrEven(num) {
  if (num % 2) { // ํ™€์ˆ˜๋ฉด
    return odd;
  }
  return even;
}

export default checkOddOrEven;
//index.mjs
import { odd, even } from './var.mjs';
import checkNumber from './func.mjs';

function checkStringOddOrEven(str) {
  if (str.length % 2) { // ํ™€์ˆ˜๋ฉด
    return odd;
  }
  return even;
}

console.log(checkNumber(10));
console.log(checkStringOddOrEven('hello'));

 

-CommonJS ๋ชจ๋“ˆ์„ ES ๋ชจ๋“ˆ ์Šคํƒ€์ผ๋กœ ๋ฐ”๊พธ๋ฉด require, exports, module.exports๋ฅผ ๊ฐ๊ฐ import, export, export default(ํ•จ์ˆ˜๋‚˜ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ ๋ฌธ๋ฒ•)๋กœ ์จ์ค€๋‹ค.

-ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋Š” .js๊ฐ€ ์•„๋‹Œ .mjs๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.

-ํŒŒ์ผ์„ ์‹คํ–‰ํ•  ๋•Œ CommonJS ๋ชจ๋“ˆ๊ณผ ๋‹ฌ๋ฆฌ ํ™•์žฅ์ž๋ฅผ ์ƒ๋žตํ•˜๋ฉด ์•ˆ ๋œ๋‹ค.

 

โ€ป ํ•œ ๊ฐ€์ง€์˜ ํ˜•์‹๋งŒ ๊ณจ๋ผ์„œ ์‚ฌ์šฉํ•˜์ž!


3-3. ๋‹ค์ด๋‚ด๋ฏน ์ž„ํฌํŠธ

 

-๋‹ค์ด๋‚ด๋ฏน ์ž„ํฌํŠธ๋Š” ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ์ค‘ ๋ชจ๋“ˆ์„ ๋™์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ์ฝ”๋“œ ๋‚ด ์–ด๋””์„œ๋„ ํ˜ธ์ถœ ๊ฐ€๋Šฅํ•˜๋‹ค.

-ES ๋ชจ๋“ˆ์€ if๋ฌธ ์•ˆ์—์„œ importํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์ด๋‚ด๋ฏน ์ž„ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

//dynamic.mjs
const a = true;
if (a) {
    const m1 = await import('./func.mjs');
    console.log(m1);
    const m2 = await import('./var.mjs');
    console.log(m2);
}

→ ES๋ชจ๋“ˆ์˜ ์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„์—์„œ๋Š” async ํ•จ์ˆ˜ ์—†์ด await๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค(CommonJS ๋ชจ๋“ˆ์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅ).

 


3-4. __filename, __dirname

ํŒŒ์ผ ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค.

 

-__filename: ํ˜„์žฌ ํŒŒ์ผ๋ช…

-__dirname: ํ˜„์žฌ ํŒŒ์ผ ๊ฒฝ๋กœ

 

โ€ป ES ๋ชจ๋“ˆ์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

โ€ป ์–ธ๋”๋ฐ”(_) ๋‘ ๊ฐœ๋ฅผ ๋ถ™์—ฌ์•ผ ํ•œ๋‹ค.


4. ๋…ธ๋“œ ๋‚ด์žฅ ๊ฐ์ฒด

๋”ฐ๋กœ ์„ค์น˜ํ•˜์ง€ ์•Š์•„๋„ ๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

4-1. global

๋ธŒ๋ผ์šฐ์ €์˜ window์™€ ๊ฐ™์€ ์ „์—ญ ๊ฐ์ฒด

 

-์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค(globla.require->require). 

-global ๊ฐ์ฒด์˜ ์†์„ฑ์— ๊ฐ’์„ ๋Œ€์ž…ํ•ด ํŒŒ์ผ ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ, ์œ ์ง€ ๋ณด์ˆ˜์— ์–ด๋ ค์›€์ด ์žˆ์–ด ๋‚จ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒŒ ์ข‹๋‹ค.


4-2. console

๋ณดํ†ต ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

-๋Œ€ํ‘œ์ ์ธ console.log ๋ฉ”์„œ๋“œ ์ด์™ธ์— ๋กœ๊น… ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

-console.time(๋ ˆ์ด๋ธ”)-console.timeEnd, console.log(๋‚ด์šฉ), console.error(์—๋Ÿฌ ๋‚ด์šฉ), console.table(๋ฐฐ์—ด), console.dir(๊ฐ์ฒด, ์˜ต์…˜), console.trace(๋ ˆ์ด๋ธ”) ๋“ฑ์ด ์žˆ๋‹ค.


4-3. ํƒ€์ด๋จธ

๋ณดํ†ต ๋””๋ฒ„๊น…์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

 

-setTimeout(์ฝœ๋ฐฑ ํ•จ์ˆ˜, ๋ฐ€๋ฆฌ์ดˆ) - clearTimeout(์•„์ด๋””): ์ฃผ์–ด์ง„ ๋ฐ€๋ฆฌ์ดˆ ์ดํ›„
-setInterval(์ฝœ๋ฐฑ ํ•จ์ˆ˜, ๋ฐ€๋ฆฌ์ดˆ) - clearInterval(์•„์ด๋””): ์ฃผ์–ด์ง„ ๋ฐ€๋ฆฌ์ดˆ ๋งˆ๋‹ค
-setImmediate(์ฝœ๋ฐฑ ํ•จ์ˆ˜) - clearImmediate(์•„์ด๋””): ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ฆ‰์‹œ

→ ๋ฐ˜ํ™˜๋œ ์•„์ด๋””๋กœ ํƒ€์ด๋จธ๋ฅผ ์ทจ์†Œํ•  ์ˆ˜ ์žˆ๋‹ค.

//timer.js
const timeout = setTimeout(() => {
  console.log('1.5์ดˆ ํ›„ ์‹คํ–‰');
}, 1500);

const interval = setInterval(() => {
  console.log('1์ดˆ๋งˆ๋‹ค ์‹คํ–‰');
}, 1000);

const timeout2 = setTimeout(() => {
  console.log('์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค');
}, 3000);

setTimeout(() => {
  clearTimeout(timeout2);
  clearInterval(interval);
}, 2500);

const immediate = setImmediate(() => {
  console.log('์ฆ‰์‹œ ์‹คํ–‰');
});

const immediate2 = setImmediate(() => {
  console.log('์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค');
});

clearImmediate(immediate2);

 

→immediate๊ฐ€ ๊ฐ€์žฅ ๋จผ์ € ์‹คํ–‰๋œ๋‹ค.


4-4. process

ํ˜„์žฌ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ๋‹ค.

 

โ‘  process.env: ์‹œ์Šคํ…œ์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜

-์ž„์˜๋กœ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ €์žฅ ๊ฐ€๋Šฅ

-์„œ๋ฒ„์˜ ์ค‘์š”ํ•œ ํ‚ค๋ฅผ ์ €์žฅํ•˜๋Š” ๊ณต๊ฐ„

-์ค‘์š”ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” process.env์˜ ์†์„ฑ์œผ๋กœ ๋Œ€์ฒด

 

โ‘ก process.nextTick(์ฝœ๋ฐฑ)

//nextTick.js
setImmediate(()=> {
    console.log('immediate');
});
process.nextTick(()=> {
    console.log('nextTick');    //๋‹ค๋ฅธ ์ฝœ๋ฐฑ๋“ค๋ณด๋‹ค ์šฐ์„ ์‹œ
});
setTimeout(()=> {
    console.log('timeout');
},0);
Promise.resolve().then(()=> console.log('promise')); //resolve๋œ Promise๋„ ๋‹ค๋ฅธ ์ฝœ๋ฐฑ ๋ณด๋‹ค ์šฐ์„ ์‹œ

→ process.nextTick์™€ Promise๋ฅผ ๋งˆ์ดํฌ๋กœํƒœ์Šคํฌ๋ผ๊ณ  ๊ตฌ๋ถ„ํ•ด์„œ ๋ถ€๋ฅธ๋‹ค.

 

โ‘ข process.exit(์ฝ”๋“œ)

์„œ๋ฒ„ ์™ธ์˜ ๋…๋ฆฝ์ ์ธ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์ˆ˜๋™์œผ๋กœ ์‹คํ–‰์ค‘์ธ ๋…ธ๋“œ๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. 

 

โ€ป ์„œ๋ฒ„ ํ™˜๊ฒฝ์—์„œ๋Š” ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.


4-5. ๊ธฐํƒ€ ๋‚ด์žฅ ๊ฐ์ฒด

 

-URL, URLSearchParams
-AbortController, FormData, fetch, Headers, Request, Response, Event, EventTarget
-TextDecoder,TextEncoder, WebAssembly

 

โ€ป ๋…ธ๋“œ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ํ™œ์šฉํ•˜์ž.

 


5. ๋…ธ๋“œ ๋‚ด์žฅ ๋ชจ๋“ˆ

 

5-1. os ๋ชจ๋“ˆ

os ๋ชจ๋“ˆ์„ ํ™œ์šฉํ•ด ์šด์˜์ฒด์ œ์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

+os ๋ชจ๋“ˆ์˜ ๋Œ€ํ‘œ์ ์ธ ๋ฉ”์„œ๋“œ

-์šด์˜์ฒด์ œ ์ •๋ณด : .arch() .platform() .type() .uptime() .hostname() .release()
-๊ฒฝ๋กœ: .homedir() .tmpdir()
-๋ฉ”๋ชจ๋ฆฌ ์ •๋ณด: .cups() .freemem() .totalmem()
-์—๋Ÿฌ ์ •๋ณด: .constants

 

→ ์ปดํ“จํ„ฐ ๋‚ด๋ถ€ ์ž์›์— ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ ๋นˆ๋„๊ฐ€ ๋†’์ง€ ์•Š๋‹ค. ํ•˜์ง€๋งŒ ์šด์˜์ฒด์ œ๋ณ„๋กœ ๋‹ค๋ฅธ ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์‹ถ์„ ๋•Œ os ๋ชจ๋“ˆ์ด ์œ ์šฉํ•˜๋‹ค.


 

5-2. path

ํด๋”์™€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์‰ฝ๊ฒŒ ์กฐ์ž‘ํ•˜๋„๋ก ๋„์™€์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

 

-์šด์˜์ฒด์ œ๋ณ„๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„์ž๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค.

//path.js
const path = require('path');

const string = __filename;

console.log('path.sep:', path.sep);	//๊ฒฝ๋กœ ๊ตฌ๋ถ„์ž(/, \)
console.log('path.delimiter:', path.delimiter);	//ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๊ตฌ๋ถ„์ž
console.log('------------------------------');
console.log('path.dirname():', path.dirname(string));	//ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ํด๋” ๊ฒฝ๋กœ
console.log('path.extname():', path.extname(string));	//ํŒŒ์ผ์˜ ํ™•์žฅ์ž
console.log('path.basename():', path.basename(string));	//ํŒŒ์ผ์ด๋ฆ„(ํ™•์žฅ์ž ํฌํ•จ)
console.log('path.basename - extname:', path.basename(string, path.extname(string)));
console.log('------------------------------');
console.log('path.parse()', path.parse(string));	//ํŒŒ์ผ ๊ฒฝ๋กœ ๋ถ„๋ฆฌ
console.log('path.format():', path.format({
  dir: 'C:\\users\\zerocho',
  name: 'path',
  ext: '.js',
}));	//path.parse()ํ•œ ๊ฐ์ฒด์˜ ํŒŒ์ผ ๊ฒฝ๋กœ ํ•ฉ์นœ๋‹ค.
console.log('path.normalize():', path.normalize('C://users\\\\zerocho\\\path.js'));	//์ •์ƒ์ ์ธ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜
console.log('------------------------------');
console.log('path.isAbsolute(C:\\):', path.isAbsolute('C:\\'));	//์ ˆ๋Œ€๊ฒฝ๋กœ or ์ƒ๋Œ€๊ฒฝ๋กœ
console.log('path.isAbsolute(./home):', path.isAbsolute('./home'));
console.log('------------------------------');
console.log('path.relative():', path.relative('C:\\users\\zerocho\\path.js', 'C:\\'));	//์ฒซ๋ฒˆ์งธ ๊ฒฝ๋กœ์—์„œ ๋‘๋ฒˆ์งธ ๊ฒฝ๋กœ๋กœ ๊ฐ€๋Š” ๋ฒ•
console.log('path.join():', path.join(__dirname, '..', '..', '/users', '.', '/zerocho'));	//ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋กœ ํ•ฉ์นจ
console.log('path.resolve():', path.resolve(__dirname, '..', 'users', '.', '/zerocho'));	//join๊ณผ ๋‹ฌ๋ฆฌ  ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ์ธ์‹ํ•ด ์•ž์˜ ๊ฒฝ๋กœ๋ฅผ ๋ฌด์‹œ

 ๋…ธ๋“œ๋Š” require.main ํŒŒ์ผ์„ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€๊ฒฝ๋กœ๋ฅผ ์ธ์‹ํ•œ๋‹ค.

์ฝ”๋“œ ์‹คํ–‰ ๊ฒฐ๊ณผ

 

โ€ป path ๋ชจ๋“ˆ์€ ๋…ธ๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•˜๋ฉฐ ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค.


5-3. url

์ธํ„ฐ๋„ท ์ฃผ์†Œ๋ฅผ ์‰ฝ๊ฒŒ ์กฐ์ž‘ํ•˜๋„๋ก ๋„์™€์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

 

WHATWG์™€ ๋…ธ๋“œ์˜ ์ฃผ์†Œ ์ฒด๊ณ„

 

โ‘  url

-WHATWG์˜ url์€ url ๋ชจ๋“ˆ ์•ˆ์— URL ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์–ด ์—ฌ๊ธฐ์— ์ฃผ์†Œ๋ฅผ ๋„ฃ์–ด ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๋ฉด ์ฃผ์†Œ๊ฐ€ ๋ถ€๋ถ„๋ณ„๋กœ ์ •๋ฆฌ๋œ๋‹ค. username, password, origin, searchParams ์†์„ฑ์ด ์กด์žฌํ•œ๋‹ค. 

-url.format(๊ฐ์ฒด): ๋ถ„ํ•ด๋˜์—ˆ๋˜ ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ์›๋ž˜ ์ƒํƒœ๋กœ ์กฐ๋ฆฝํ•œ๋‹ค. 

 

โ‘ก searchParams ๊ฐ์ฒด

-search ๋ถ€๋ถ„์€ ์ฃผ์†Œ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. 

-์—ฌ๋Ÿฌ ํ‚ค๊ฐ€์žˆ์„ ๋•Œ๋Š” &๋กœ ๊ตฌ๋ถ„ํ•œ๋‹ค.

-ํ‚ค=๊ฐ’ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.  

//searchParams.js
const myURL = new URL('http://www.gilbut.co.kr/?page=3&limit=10&category=nodejs&category=javascript');
console.log('searchParams:', myURL.searchParams);
console.log('searchParams.getAll():', myURL.searchParams.getAll('category'));	//ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๊ฐ’ ๊ฐ€์ ธ์˜ด
console.log('searchParams.get():', myURL.searchParams.get('limit'));	//ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ๊ฐ’ ๊ฐ€์ ธ์˜ด
console.log('searchParams.has():', myURL.searchParams.has('page'));	//ํ•ด๋‹น ํ‚ค์˜ ์กด์žฌ ๊ฒ€์‚ฌ

console.log('searchParams.keys():', myURL.searchParams.keys());	//๋ชจ๋“  ํ‚ค๋ฅผ ๋ฐ˜๋ณต๊ธฐ ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ด
console.log('searchParams.values():', myURL.searchParams.values());	//๋ชจ๋“  ๊ฐ’์„ ๋ฐ˜๋ณต๊ธฐ ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ด

myURL.searchParams.append('filter', 'es3');	//ํ•ด๋‹น ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•จ
myURL.searchParams.append('filter', 'es5');
console.log(myURL.searchParams.getAll('filter'));

myURL.searchParams.set('filter', 'es6');	//๊ฐ™์€ ํ‚ค์˜ ๊ฐ’๋“ค์„ ์ง€์šฐ๊ณ  ์ƒˆ๋กœ ์ถ”๊ฐ€
console.log(myURL.searchParams.getAll('filter'));

myURL.searchParams.delete('filter');	//ํ•ด๋‹น ํ‚ค ์ œ๊ฑฐ
console.log(myURL.searchParams.getAll('filter'));

console.log('searchParams.toString():', myURL.searchParams.toString());
myURL.search = myURL.searchParams.toString();	//์กฐ์ž‘ํ•œ searchParams ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ ๋‹ค

5-4. dns

๋„๋ฉ”์ธ์„ ํ†ตํ•ด IP๋‚˜ ๊ธฐํƒ€ DNS ์ •๋ณด๋ฅผ ์–ป๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

-ip์ฃผ์†Œ ์–ป๊ธฐ - dns.lookup  dns.resolve
-A(ipv4์ฃผ์†Œ),  AAAA(ipv6์ฃผ์†Œ), NS(๋„ค์ž„์„œ๋ฒ„), SOA(๋„๋ฉ”์ธ ์ •๋ณด), CNAME(๋ณ„์นญ), MX(๋ฉ”์ผ ์„œ๋ฒ„)


5-5. crypto

๋‹ค์–‘ํ•œ ๋ฐฉ์‹์˜ ์•”ํ˜ธํ™”๋ฅผ ๋„์™€์ฃผ๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

 

โ‘  ๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™”

-๋ณดํ†ต ๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ์•”ํ˜ธํ™”ํ•œ๋‹ค.

-๋ณตํ˜ธํ™”ํ•  ์ˆ˜ ์—†๋Š” ์•”ํ˜ธํ™” ๋ฐฉ์‹์ด๋‹ค(ํ•œ ๋ฒˆ ์•”ํ˜ธํ™” ํ•˜๋ฉด ์›๋ž˜ ๋ฌธ์ž์—ด์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค.)

-์ฃผ๋กœ ํ•ด์‹œ ๊ธฐ๋ฒ•(์–ด๋– ํ•œ ๋ฌธ์ž์—ด์„ ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•)์„ ์‚ฌ์šฉํ•œ๋‹ค.

//hash.js
const crypto = require('crypto');

console.log('base64:', crypto.createHash('sha512').update('๋น„๋ฐ€๋ฒˆํ˜ธ').digest('base64'));
console.log('hex:', crypto.createHash('sha512').update('๋น„๋ฐ€๋ฒˆํ˜ธ').digest('hex'));
console.log('base64:', crypto.createHash('sha512').update('๋‹ค๋ฅธ ๋น„๋ฐ€๋ฒˆํ˜ธ').digest('base64'));

 

-createHash(์•Œ๊ณ ๋ฆฌ์ฆ˜): ์‚ฌ์šฉํ•  ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋„ฃ๋Š”๋‹ค.

-update(๋ฌธ์ž์—ด): ๋ณ€ํ™˜ํ•  ๋ฌธ์ž์—ด์„ ๋„ฃ๋Š”๋‹ค.

-digest(์ธ์ฝ”๋”ฉ): ์ธ์ฝ”๋”ฉํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋„ฃ๋Š”๋‹ค. base64, hex, latin1์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. 

 

→ ์ปดํ“จํ„ฐ์˜ ์„ฑ๋Šฅ์ด ๋ฐœ๋‹ฌํ•จ์— ๋”ฐ๋ผ ๊ธฐ์กด ํ•ด์‹œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์ด ์œ„ํ˜‘๋ฐ›๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ๋” ๊ฐ•๋ ฅํ•˜๊ฒŒ ์ง„ํ™”ํ•˜๊ณ  ์žˆ๋‹ค. ํ˜„์žฌ๋Š” ์ฃผ๋กœ pbkdf2๋‚˜ bcrypt, scrypt ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•œ๋‹ค. 

 

โ‘ก ์–‘๋ฐฉํ–ฅ ์•”ํ˜ธํ™”

-์•”ํ˜ธํ™”๋œ ๋ฌธ์ž์—ด์„ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด ๋ณตํ˜ธํ™” ๊ฐ€๋Šฅํ•˜๋‹ค.

-์•”ํ˜ธ๋ฅผ ๋ณตํ˜ธํ™” ํ•˜๋ ค๋ฉด ์•”ํ˜ธํ™” ํ•  ๋•Œ ์‚ฌ์šฉํ•œ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

//cipher.js
const crypto = require('crypto');

const algorithm = 'aes-256-cbc';
const key = 'abcdefghijklmnopqrstuvwxyz123456';
const iv = '1234567890123456';

const cipher = crypto.createCipheriv(algorithm, key, iv);
let result = cipher.update('์•”ํ˜ธํ™”ํ•  ๋ฌธ์žฅ', 'utf8', 'base64');
result += cipher.final('base64');
console.log('์•”ํ˜ธํ™”:', result);

const decipher = crypto.createDecipheriv(algorithm, key, iv);
let result2 = decipher.update(result, 'base64', 'utf8');
result2 += decipher.final('utf8');
console.log('๋ณตํ˜ธํ™”:', result2);

-crypto.createCipheriv(์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ‚ค, iv)
-cipher.update(๋ฌธ์ž์—ด, ์ธ์ฝ”๋”ฉ, ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ)
-cipher.final(์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ)
-crypto.createDecipheriv(์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ‚ค, iv)
-decipher.update(๋ฌธ์ž์—ด, ์ธ์ฝ”๋”ฉ, ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ)
-decipher.final(์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ)

 

โ€ป ์ถ”๊ฐ€๋กœ ๋…ธ๋“œ ๊ณต์‹ ๋ฌธ์„œ, npmํŒจํ‚ค์ง€ ํ™•์ธํ•ด ์•Œ์•„๋ณธ๋‹ค.


5-6. util

๊ฐ์ข… ํŽธ์˜ ๊ธฐ๋Šฅ์„ ๋ชจ์•„๋‘” ๋ชจ๋“ˆ์ด๋‹ค.

 

-util.deprecate: ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ๋„ฃ์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜์ธ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.
-util.promisify: ์ฝœ๋ฐฑ ํŒจํ„ด์„ ํ”„๋กœ๋ฏธ์Šค ํŒจํ„ด์œผ๋กœ ๋ฐ”๊พผ๋‹ค. ๋ฐ”๊ฟ€ ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ์ œ๊ณตํ•œ๋‹ค. 


5-7. worker_threads

 

-๋…ธ๋“œ์—์„œ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ๋ฐฉ์‹ ์ž‘์—…์ด๋ž€? 

→์›Œ์ปค ์Šค๋ ˆ๋“œ(์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ ๋‹จ์œ„)๋ฅผ ๋„์ž…ํ•ด ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋”ฉ์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. ์ฆ‰, ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ์™€ ๋ณ„๋„๋กœ ์ž‘๋™ํ•˜๋Š” ์›Œ์ปค ์Šค๋ ˆ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ณต์žกํ•œ ์ž‘์—…์ด๋‚˜ ๊ณ„์‚ฐ์„ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์™€ ๋…๋ฆฝ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

//worker_data.js
const {
  Worker, isMainThread, parentPort, workerData,
} = require('worker_threads');

if (isMainThread) { // ๋ถ€๋ชจ์ผ ๋•Œ
  const threads = new Set();
  threads.add(new Worker(__filename, {
    workerData: { start: 1 },
  }));
  threads.add(new Worker(__filename, {
    workerData: { start: 2 },
  }));
  for (let worker of threads) {
    worker.on('message', message => console.log('from worker', message));
    worker.on('exit', () => {
      threads.delete(worker);
      if (threads.size === 0) {
        console.log('job done');
      }
    });
  }
} else { // ์›Œ์ปค์ผ ๋•Œ
  const data = workerData;
  parentPort.postMessage(data.start + 100);
}

→ ํ˜„์žฌ ๋‘ ๊ฐœ์˜ ์›Œ์ปค๊ฐ€ ๋Œ์•„๊ฐ€๊ณ  ์žˆ์œผ๋ฉฐ, ๊ฐ๊ฐ์˜ ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ์ˆซ์ž๋ฅผ ๋ฐ›์•„์„œ 100์„ ๋”ํ•ด ๋Œ๋ ค์ค€๋‹ค. (์—ฌ๋Ÿฌ ๊ฐœ์˜ ์›Œ์ปค ์Šค๋ ˆ๋“œ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ธฐ๋Š” ์˜ˆ์ œ)

 

//prime-worker.js
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

const min = 2;
let primes = [];

function findPrimes(start, end) {
  let isPrime = true;
  for (let i = start; i <= end; i++) {
    for (let j = min; j < Math.sqrt(end); j++) {
      if (i !== j && i % j === 0) {
        isPrime = false;
        break;
      }
    }
    if (isPrime) {
      primes.push(i);
    }
    isPrime = true;
  }
}

if (isMainThread) {
  const max = 10000000;
  const threadCount = 8;
  const threads = new Set();
  const range = Math.floor((max - min) / threadCount);
  let start = min;
  console.time('prime');
  for (let i = 0; i < threadCount - 1; i++) {
    const end = start + range - 1;
    threads.add(new Worker(__filename, { workerData: { start, range: end } }));
    start += range;
  }
  threads.add(new Worker(__filename, { workerData: { start, range: max } }));
  for (let worker of threads) {
    worker.on('error', (err) => {
      throw err;
    });
    worker.on('exit', () => {
      threads.delete(worker);
      if (threads.size === 0) {
        console.timeEnd('prime');
        console.log(primes.length);
      }
    });
    worker.on('message', (msg) => {
      primes = primes.concat(msg);
    });
  }
} else {
  findPrimes(workerData.start, workerData.range);
  parentPort.postMessage(primes);
}

→ ์†Œ์ˆ˜๊ฐ€ ๋ช‡ ๊ฐœ ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ด๋Š” ์ฝ”๋“œ์ด๋‹ค. ์›Œ์ปค ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์—ฌ๋Ÿฌ  ๊ฐœ์˜ ์Šค๋ ˆ๋“œ๊ฐ€ ๋‚˜๋ˆ ์„œ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ๊ฒƒ๋ณด๋‹ค ์†๋„๊ฐ€ ๋นจ๋ผ์ง„๋‹ค.

 


5-8. child_process

๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ์‹ถ๊ฑฐ๋‚˜ ๋ช…๋ น์–ด๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ชจ๋“ˆ์ด๋‹ค.

 

-๋‹ค๋ฅธ ์–ธ์–ด์˜ ์ฝ”๋“œ(ํŒŒ์ด์ฌ ๋“ฑ)๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ด๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. 

//exec.js
const exec = require('child_process').exec;

const process = exec('dir');

process.stdout.on('data', function(data) {
  console.log(data.toString());
}); // ์‹คํ–‰ ๊ฒฐ๊ณผ

process.stderr.on('data', function(data) {
  console.error(data.toString());
}); // ์‹คํ–‰ ์—๋Ÿฌ

→ ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์˜ ๋ช…๋ น์–ด์ธ dir์„ ํ†ตํ•ด ๋…ธ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

//spawn.js
const spawn = require('child_process').spawn;

const process = spawn('python', ['test.py']);	//์ธ์ˆ˜: (๋ช…๋ น์–ด, ๋ฐฐ์—ด)

process.stdout.on('data', function(data) {
  console.log(data.toString());
}); // ์‹คํ–‰ ๊ฒฐ๊ณผ

process.stderr.on('data', function(data) {
  console.error(data.toString());
}); // ์‹คํ–‰ ์—๋Ÿฌ

→ ํŒŒ์ด์ฌ ์ฝ”๋“œ๋ฅผ ๋…ธ๋“œ์˜ spawn์„ ํ†ตํ•ด ์‹คํ–‰ํ•œ๋‹ค. 

 

-exec์™€ spawn์˜ ์ฐจ์ด๋Š”?

→ exec๋Š” ์…ธ์—์„œ ์‹คํ–‰ํ•ด์„œ ๋ช…๋ น์–ด ์ˆ˜ํ–‰, spawn์€ ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์Šค๋ฅผ ๋„์šฐ๋ฉด์„œ ๋ช…๋ น์–ด ์‹คํ–‰ํ•œ๋‹ค(์…ธ์—์„œ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์„ธ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ {shell:true}๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 


5-9. ๊ธฐํƒ€ ๋ชจ๋“ˆ๋“ค

 

โ€ป ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•ด ํ™œ์šฉํ•˜๊ธฐ!


Quiz

1. ๋…ธ๋“œ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ฆ‰์„์—์„œ ์‹คํ–‰ํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•˜๋Š” ํ™˜๊ฒฝ์€ (    )์ด๋‹ค.

2. ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋Š” ๋ช…๋ น์–ด๋Š” (    )์ด๋‹ค.

3. ํ‘œ์ค€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ์€ ์•„๋‹ˆ์ง€๋งŒ ๊ฐ€์žฅ ๋„๋ฆฌ ์“ฐ์ด๋Š” ๋ชจ๋“ˆ์€ (        ) ๋ชจ๋“ˆ์ด๋‹ค.

4. ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ•จ์ˆ˜๋Š” (       )์ด๋‹ค.

5. ๋…ธ๋“œ์—์„œ ํ˜„์žฌ ํŒŒ์ผ๋ช…๊ณผ ํ˜„์žฌ ํŒŒ์ผ ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ‚ค์›Œ๋“œ๋Š” (        ), (       )์ด๋‹ค.

6. ์ฃผ์–ด์ง„ ๋ฐ€๋ฆฌ์ดˆ ์ดํ›„์— ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋…ธ๋“œ ๋‚ด์žฅ ๊ฐ์ฒด๋Š” (        )์ด๋‹ค.

7. ํด๋”์™€ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๋ฅผ ์‰ฝ๊ฒŒ ์กฐ์ž‘ํ•˜๋„๋ก ๋„์™€์ฃผ๋Š” ๋ชจ๋“ˆ์€ (    )์ด๋‹ค.

 

Programming Quiz

8. ์•„๋ž˜์˜ CommonJS ๋ชจ๋“ˆ ๋ฐฉ์‹์œผ๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋ฅผ ES ๋ชจ๋“ˆ ์Šคํƒ€์ผ๋กœ ๋ฐ”๊ฟ”์„œ ์ž‘์„ฑํ•˜์‹œ์˜ค.

//index.js
const { odd, even } = require('./var');
const checkNumber = require('./func');

function checkStringOddOrEven(str) {
  if (str.length % 2) { 
  }
  return even;
}

console.log(checkNumber(10));
console.log(checkStringOddOrEven('node.js_team2'));

 

9. process ๋‚ด์žฅ ๊ฐ์ฒด์˜ process.exit()์™€ setInterval ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด 0.5์ดˆ๋งˆ๋‹ค 5๋ถ€ํ„ฐ 1๊นŒ์ง€ ํ‘œ์‹œํ•œ ๋’ค, i๊ฐ€ 1์ด ๋˜์—ˆ์„ ๋•Œ ์ข…๋ฃŒํ•˜๋„๋ก ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


Answer

1. REPL

2. node

3. CommonJS

4. require

5. __filename, __dirname

6. setTimeout()

7. path

8.

//index.mjs
import { odd, even } from './var.mjs';
import checkNumber from './func.mjs';

function checkStringOddOrEven(str) {
  if (str.length % 2) { // ํ™€์ˆ˜๋ฉด
    return odd;
  }
  return even;
}

console.log(checkNumber(10));
console.log(checkStringOddOrEven('node.js_team2'));

9.

//9
let i=5;
setInterval(() => {
    if (i===1) {
        console.log('์ข…๋ฃŒ');
        process.exit();
    }
    console.log(i);
    i--;
}, 500);

 

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

 

Corner node.js 2ํŒ€

Editor : ์ด๊ตฌ๋ฏธ



728x90

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