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

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋…ธ๋“œ 2] 3์žฅ. ๋…ธ๋“œ ๊ธฐ๋Šฅ ์•Œ์•„๋ณด๊ธฐ (1)

23-24/Node.js 2

by _๋„๋‹ด 2023. 10. 6. 10:00

๋ณธ๋ฌธ

728x90

 

๐ŸŒŸ3์žฅ(1) ํ‚ค์›Œ๋“œ๐ŸŒŸ

REPL

js ํŒŒ์ผ ์‹คํ–‰ํ•˜๊ธฐ

๋…ธ๋“œ ๋ชจ๋“ˆ; CommonJS ๋ชจ๋“ˆ, ECMAScript ๋ชจ๋“ˆ

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

 

 

3.1 REPL ์‚ฌ์šฉํ•˜๊ธฐ

js๋Š” ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด (์ปดํŒŒ์ผ ๋ถˆํ•„์š”)
Read Eval Print Loop
์ž…๋ ฅํ•œ ์ฝ”๋“œ๋ฅผ ์ฝ๊ณ , ํ•ด์„ํ•˜๊ณ , ๊ฒฐ๊ณผ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ข…๋ฃŒํ•  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค

ํ„ฐ๋ฏธ๋„์—์„œ node ๋ช…๋ น ์ž…๋ ฅ ์‹œ
> ๋ชจ์–‘์˜ ํ”„๋กฌํ”„ํŠธ
js ์ฝ”๋“œ ์ž…๋ ฅ ๊ฐ€๋Šฅ

 

 

3.2 JS ํŒŒ์ผ ์‹คํ–‰ํ•˜๊ธฐ

js ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ์‹คํ–‰

function helloWorld() {
    console.log('Hello World');
    helloNode();
  }
  
  function helloNode() {
    console.log('Hello Node');
  }
  
  helloWorld();
 

node ๋ช…๋ น์–ด๋กœ ํŒŒ์ผ ์‹คํ–‰(.js ํ™•์žฅ์ž ์ƒ๋žต ๊ฐ€๋Šฅ)

 

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

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

  • ๋ชจ๋“ˆ์€ ๊ทธ ์ž์ฒด๋กœ๋„ ํ•˜๋‚˜์˜ ํ”„๋กœ๊ทธ๋žจ์ด๋ฉด์„œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์˜ ๋ถ€ํ’ˆ์œผ๋กœ ์‚ฌ์šฉ/์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ๋ณดํ†ต ํŒŒ์ผ ํ•˜๋‚˜ ๋‹จ์œ„๋กœ ๋ชจ๋“ˆ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ฆ

๋…ธ๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ชจ๋“ˆ ํ˜•์‹

  • CommonJS ๋ชจ๋“ˆ
  • ECMAScript ๋ชจ๋“ˆ

1) CommonJS ๋ชจ๋“ˆ

  • js ํ‘œ์ค€์ด ๋‚˜์˜ค๊ธฐ ์ด์ „๋ถ€ํ„ฐ ์‚ฌ์šฉ๋จ. ์ดํ›„๋กœ๋„ ๋…ธ๋“œ ์ƒํƒœ๊ณ„์—์„œ ๊ฐ€์žฅ ๋„๋ฆฌ ์“ฐ์ž„.
// 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'));
 

module ๊ฐ์ฒด ๋Œ€์‹  exports ๊ฐ์ฒด ์‚ฌ์šฉํ•˜๊ธฐ

  • module.exports === exports ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์ด๋ฆ„์ด๋‹ค!
// var2.js
exports.odd = 'CommonJS ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค';
exports.even = 'CommonJS ์ง์ˆ˜์ž…๋‹ˆ๋‹ค';
 

์œ ์˜์‚ฌํ•ญ

  • ๋‘ ๊ฐ์ฒด ๊ฐ„์˜ ์ฐธ์กฐ ๊ด€๊ณ„๊ฐ€ ๊นจ์ง€์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•จ!
  • module.exports์—๋Š” ์–ด๋–ค ๊ฐ’์ด๋“  ๋Œ€์ž… ๊ฐ€๋Šฅ
  • exports์—๋Š” ๋ฐ˜๋“œ์‹œ ๊ฐ์ฒด์ฒ˜๋Ÿผ exports.์†์„ฑ๋ช…=์†์„ฑ๊ฐ’ ํ˜•ํƒœ๋กœ ๋Œ€์ž… (๋‹ค๋ฅธ ๊ฐ’ ๋Œ€์ž… ์‹œ ์ฐธ์กฐ ๊ด€๊ณ„๊ฐ€ ๋Š๊ฒจ ๋”๋Š” ๋ชจ๋“ˆ๋กœ ๊ธฐ๋Šฅํ•˜์ง€ ์•Š์Œ)

[Note] ๋…ธ๋“œ์—์„œ์˜ this

  • ์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„์— ์กด์žฌํ•˜๋Š” this๋Š” module.exports(๋˜๋Š” export ๊ฐ์ฒด)๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  • ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ ๋‚ด๋ถ€์˜ this๋Š” global ๊ฐ์ฒด(3.4.1์ ˆ)๋ฅผ ๊ฐ€๋ฆฌํ‚ด

require

  • require๋Š” ํ•จ์ˆ˜์ด๊ณ , ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด์ด๋ฏ€๋กœ (!!) require๋Š” ๊ฐ์ฒด๋กœ์„œ ์†์„ฑ์„ ๋ช‡ ๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • require.cache : ํ•œ ๋ฒˆ requireํ•œ ํŒŒ์ผ์ด ์ €์žฅ๋˜๋Š” ๊ณณ
  • require.main : ๋…ธ๋“œ ์‹คํ–‰ ์‹œ ์ฒซ ๋ชจ๋“ˆ์„ ๊ฐ€๋ฆฌํ‚ด.
  • ์•„๋ž˜ ์ฝ˜์†”์—์„œ node require ์‹คํ–‰ํ–ˆ์œผ๋ฏ€๋กœ require.js๊ฐ€ require.main์ด ๋จ
console.log('require๊ฐ€ ๊ฐ€์žฅ ์œ„์— ์˜ค์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค.');

module.exports = '์ €๋ฅผ ์ฐพ์•„๋ณด์„ธ์š”.';

require('./var');

console.log('require.cache์ž…๋‹ˆ๋‹ค.');
console.log(require.cache);
console.log('require.main์ž…๋‹ˆ๋‹ค.');
console.log(require.main === module);
console.log(require.main.filename);
 

๋ชจ๋“ˆ ์‚ฌ์šฉ ์‹œ ์ฃผ์˜ - dep1๊ณผ dep2๊ฐ€ ์„œ๋กœ๋ฅผ requireํ•œ๋‹ค๋ฉด?

// dep1.js
const dep2 = require('./dep2');
console.log('require dep2', dep2);
module.exports = () => {
  console.log('dep2', dep2);
};
// dep2.js
const dep1 = require('./dep1');
console.log('require dep1', dep1);
module.exports = () => {
  console.log('dep1', dep1);
};
//dep-run.js
const dep1 = require('./dep1');
const dep2 = require('./dep2');

dep1();
dep2();

node dep-run ์‹คํ–‰ ๊ฒฐ๊ณผ

  • ์ด์ฒ˜๋Ÿผ ์ˆœํ™˜ ์ฐธ์กฐ ๋ฐœ์ƒ ์‹œ module.exports ํ•จ์ˆ˜๊ฐ€ ๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด๋กœ ๋ณ€ํ•จ!
  • ์ˆœํ™˜ ์ฐธ์กฐ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก ๊ตฌ์กฐ๋ฅผ ์ž˜ ์žก๋Š” ๊ฒƒ์ด ์ค‘์š”...

 

2) ECMAScript ๋ชจ๋“ˆ

  • ๊ณต์‹์ ์ธ JS ๋ชจ๋“ˆ ํ˜•์‹

๋‹ค๋ฅธ ๋ถ€๋ถ„

  • require (ํ•จ์ˆ˜) → import (๋ฌธ๋ฒ•)
  • exports (๊ฐ์ฒด) → export (๋ฌธ๋ฒ•)
  • module.exports (๊ฐ์ฒด) → export default (๋ฌธ๋ฒ•)
// 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 ๋ชจ๋“ˆ์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅ
// dynamic.js
const a = false;
if (a) {
    require('./func');  // ์‹คํ–‰๋˜์ง€ ์•Š์Œ
}
console.log('์„ฑ๊ณต');

// dynamic.mjs
const a = false;
if (a) {
    import './func.mjs';  // ES ๋ชจ๋“ˆ์€ if๋ฌธ ์•ˆ์—์„œ import ๋ถˆ๊ฐ€
}
console.log('์„ฑ๊ณต');
  • ๋‹ค์ด๋‚ด๋ฏน ์ž„ํฌํŠธ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •
  • import ๋ฌธ๋ฒ• ๋Œ€์‹  import() ํ•จ์ˆ˜ ์‚ฌ์šฉ
  • ES ๋ชจ๋“ˆ์˜ ์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„์—์„œ๋Š” async ํ•จ์ˆ˜ ์—†์ด๋„ await ๊ฐ€๋Šฅ
// 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);
}

+) __filename, __dirname

์‹คํ–‰ ์‹œ ํ˜„์žฌ ํŒŒ์ผ๋ช…, ํ˜„์žฌ ํŒŒ์ผ ๊ฒฝ๋กœ๋กœ ๋ฐ”๋€œ

// filename.js
console.log(__filename);
console.log(__dirname);

  • ES ๋ชจ๋“ˆ์—์„œ๋Š” __filename, __dirname ์‚ฌ์šฉ ๋ถˆ๊ฐ€, ๋Œ€์‹  import.meta.url๋กœ ๊ฒฝ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ
// filename.mjs
console.log(import.meta.url);
console.log('__filename์€ ์—๋Ÿฌ');
console.log(__filename);

 

3.4 ๋…ธ๋“œ ๋‚ด์žฅ ๊ฐ์ฒด ์•Œ์•„๋ณด๊ธฐ

1) global

  • ๋ธŒ๋ผ์šฐ์ €์˜ window์™€ ๊ฐ™์€ ์ „์—ญ ๊ฐ์ฒด
  • ๋งค์šฐ ๋งŽ์€ ์†์„ฑ์„ ํฌํ•จํ•จ (ํ™•์ธํ•˜๋ ค๋ฉด REPL(์ฝ˜์†”) ์ด์šฉ)

 

// globalA.js
module.exports = () => global.message;
// globalB.js
const A = require('./globalA');

global.message = '์•ˆ๋…•ํ•˜์„ธ์š”';
console.log(A());

globalB์—์„œ ๋„ฃ์€ global.message ๊ฐ’์„ globalA์—์„œ๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅ

 

2) console

๋กœ๊น… ํ•จ์ˆ˜ ์ข…๋ฅ˜

  • console.time(๋ ˆ์ด๋ธ”) : console.timeEnd(๋ ˆ์ด๋ธ”)๊ณผ ๋Œ€์‘๋˜์–ด ๊ฐ™์€ ๋ ˆ์ด๋ธ”์„ ๊ฐ€์ง„ time๊ณผ timeEnd ์‚ฌ์ด์˜ ์‹œ๊ฐ„์„ ์ธก์ •
  • console.log(๋‚ด์šฉ) : ํ‰๋ฒ”ํ•œ ๋กœ๊ทธ๋ฅผ ์ฝ˜์†”์— ํ‘œ์‹œ. console.log(๋‚ด์šฉ, ๋‚ด์šฉ, ...) ์ฒ˜๋Ÿผ ์—ฌ๋Ÿฌ ๋‚ด์šฉ์„ ๋™์‹œ์— ํ‘œ์‹œ ๊ฐ€๋Šฅ
  • console.error(์—๋Ÿฌ ๋‚ด์šฉ) : ์—๋Ÿฌ๋ฅผ ์ฝ˜์†”์— ํ‘œ์‹œ
  • console.table(๋ฐฐ์—ด) : ๋ฐฐ์—ด์˜ ์š”์†Œ๋กœ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ๋„ฃ์œผ๋ฉด, ๊ฐ์ฒด์˜ ์†์„ฑ๋“ค์ด ํ…Œ์ด๋ธ” ํ˜•์‹์œผ๋กœ ํ‘œํ˜„๋จ
  • console.dir(๊ฐ์ฒด, ์˜ต์…˜) : ๊ฐ์ฒด๋ฅผ ์ฝ˜์†”์— ํ‘œ์‹œํ•  ๋•Œ ์‚ฌ์šฉ. ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ํ‘œ์‹œํ•  ๊ฐ์ฒด๋ฅผ ๋„ฃ๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์˜ต์…˜์„ ๋„ฃ์Œ. colors=true๋กœ ํ•˜๋ฉด ์ฝ˜์†”์— ์ƒ‰์ด ์ถ”๊ฐ€๋จ, depth๋Š” ๊ฐ์ฒด ์•ˆ์˜ ๊ฐ์ฒด๋ฅผ ๋ช‡ ๋‹จ๊ณ„๊นŒ์ง€ ๋ณด์—ฌ์ค„์ง€ ๊ฒฐ์ •, ๊ธฐ๋ณธ๊ฐ’์€ 2
  • console.trace(๋ ˆ์ด๋ธ”) : ์—๋Ÿฌ๊ฐ€ ์–ด๋””์—์„œ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์ถ”์ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•จ. ์—๋Ÿฌ ๋ฐœ์ƒ ์œ„์น˜๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์“ธ๋งŒํ•จ!
const string = 'abc';
const number = 1;
const boolean = true;
const obj = {
  outside: {
    inside: {
      key: 'value',
    },
  },
};
console.time('์ „์ฒด์‹œ๊ฐ„');
console.log('ํ‰๋ฒ”ํ•œ ๋กœ๊ทธ์ž…๋‹ˆ๋‹ค ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•ด ์—ฌ๋Ÿฌ ๊ฐ’์„ ์ฐ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค');
console.log(string, number, boolean);
console.error('์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋Š” console.error์— ๋‹ด์•„์ฃผ์„ธ์š”');

console.table([{ name: '์ œ๋กœ', birth: 1994 }, { name: 'hero', birth: 1988}]);

console.dir(obj, { colors: false, depth: 2 });
console.dir(obj, { colors: true, depth: 1 });

console.time('์‹œ๊ฐ„์ธก์ •');
for (let i = 0; i < 100000; i++) {}
console.timeEnd('์‹œ๊ฐ„์ธก์ •');

function b() {
  console.trace('์—๋Ÿฌ ์œ„์น˜ ์ถ”์ ');
}
function a() {
  b();
}
a();

console.timeEnd('์ „์ฒด์‹œ๊ฐ„');

3) ํƒ€์ด๋จธ

ํƒ€์ด๋จธ ์„ค์ • ํ•จ์ˆ˜

  • setTimeout(์ฝœ๋ฐฑํ•จ์ˆ˜, ๋ฐ€๋ฆฌ์ดˆ) : ๋ฐ€๋ฆฌ์ดˆ ํ›„ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์‹คํ–‰
  • setInterval(์ฝœ๋ฐฑํ•จ์ˆ˜, ๋ฐ€๋ฆฌ์ดˆ) : ๋ฐ€๋ฆฌ์ดˆ๋งˆ๋‹ค ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋ฐ˜๋ณต ์‹คํ–‰
  • setImmediate(์ฝœ๋ฐฑํ•จ์ˆ˜) : ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ฆ‰์‹œ์‹คํ–‰

ํƒ€์ด๋จธ ์ทจ์†Œ ํ•จ์ˆ˜

  • clearTimeout(์•„์ด๋””)
  • clearInterval(์•„์ด๋””)
  • 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);

Promise ๊ธฐ๋ฐ˜ ํƒ€์ด๋จธ (๋…ธ๋“œ ๋‚ด์žฅ ๋ชจ๋“ˆ)

  • setInterval(๋ฐ€๋ฆฌ์ดˆ, ์‹œ์ž‘๊ฐ’)์€ for await of ๋ฌธ๋ฒ•๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
// timerPromise.js
import { setTimeout, setInterval } from 'timers/promises';

await setTimeout(3000);
console.log('3์ดˆ๋’ค ์‹คํ–‰');

for await (const startTime of setInterval(1000, Date.now())) {
    console.log('1์ดˆ๋งˆ๋‹ค ์‹คํ–‰', new Date(startTime));
}

4) process

process ๊ฐ์ฒด: ํ˜„์žฌ ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์Œ
REPL์— ์ž…๋ ฅํ•ด ํ™•์ธ ๊ฐ€๋Šฅ

process.env

ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ํ™•์ธ ๋ช…๋ น
process.env์— ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ 6.2์ ˆ dotenv ์‚ฌ์šฉ๋ฒ• ์ฐธ์กฐ

process.nextTick(์ฝœ๋ฐฑ)

  • ์ด๋ฒคํŠธ ๋ฃจํ”„๊ฐ€ ๋‹ค๋ฅธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋“ค๋ณด๋‹ค nextTick์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์šฐ์„ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋งŒ๋“ฆ
  • resolve๋œ Promise๋„ ๋‹ค๋ฅธ ์ฝœ๋ฐฑ๋“ค๋ณด๋‹ค ์šฐ์„ ์‹œ ๋จ
// nextTick.js
setImmediate(() => {
  console.log('immediate');
});
process.nextTick(() => {
  console.log('nextTick');
});
setTimeout(() => {
  console.log('timeout');
}, 0);
Promise.resolve().then(() => console.log('promise'));

process.exit(์ฝ”๋“œ)

์‹คํ–‰ ์ค‘์ธ ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒ

  • process.exit() ์ •์ƒ ์ข…๋ฃŒ
  • process.exit(0) ์ •์ƒ ์ข…๋ฃŒ
  • process.exit(1) ๋น„์ •์ƒ ์ข…๋ฃŒ

5) ๊ธฐํƒ€ ๋‚ด์žฅ ๊ฐ์ฒด

  • URL, URLSearchParams : 3.5์ ˆ
  • AbortController, FormData, fetch, Headers, Request, Response, Event, EventTarget: ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‚ฌ์šฉํ•˜๋˜ API๊ฐ€ ๋…ธ๋“œ์—๋„ ๋™์ผํ•˜๊ฒŒ ์žˆ์Œ
  • TextDecoder : Buffer๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟˆ
  • TextEncoder: ๋ฌธ์ž์—ด์„ Buffer๋กœ ๋ฐ”๊ฟˆ
  • WebAssembly : ์›น์–ด์…ˆ๋ธ”๋ฆฌ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹น

Quiz.

๋นˆ์นธ ๋ฌธ์ œ (๋นˆ์นธ ๋“œ๋ž˜๊ทธ ์‹œ ์ •๋‹ต ํ‘œ์‹œ)

  1. CommonJS ๋ชจ๋“ˆ ํ˜•์‹์—์„œ, module.exports์™€ (exports)๋Š” ๋™์ผํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  2. exports์—๋Š” ๋ฐ˜๋“œ์‹œ (๊ฐ์ฒด์ฒ˜๋Ÿผ exports.์†์„ฑ๋ช…=์†์„ฑ๊ฐ’)ํ˜•ํƒœ๋กœ ๋Œ€์ž…ํ•˜์—ฌ ์ฐธ์กฐ ๊ด€๊ณ„๊ฐ€ ๋Š๊ธฐ์ง€ ์•Š๋„๋ก ์œ ์˜ํ•œ๋‹ค.
  3. ๋…ธ๋“œ์—์„œ์˜ this: ์ตœ์ƒ์œ„ ์Šค์ฝ”ํ”„์— ์กด์žฌํ•˜๋Š” this๋Š” module.exports๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ ๋‚ด๋ถ€์˜ this๋Š” (global ๊ฐ์ฒด)๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    https://www.zerocho.com/category/NodeJS/post/5b67e8607bbbd3001b43fd7b
  4. ๋ชจ๋“ˆ ์ˆœํ™˜ ์ฐธ์กฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด (module.exports ํ•จ์ˆ˜)๊ฐ€ (๋น„์–ด์žˆ๋Š” ๊ฐ์ฒด)๋กœ ์กฐ์šฉํžˆ ๋ณ€ํ•œ๋‹ค. ์ˆœํ™˜ ์ฐธ์กฐ๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก ๊ตฌ์กฐ๋ฅผ ์ž˜ ์žก๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.
  5. ECMAScript ๋ชจ๋“ˆ์€ ๊ณต์‹์ ์ธ ๋ชจ๋“ˆ ํ˜•์‹์œผ๋กœ, CommonJS์˜ require, exports, module.exports ๋Œ€์‹  (import, export, export default)๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ชจ๋“ˆ์„ ๋‚ด๋ณด๋‚ด๊ณ  ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ์‹์ด ๊ฐ์ฒด๋ฅผ ํ†ตํ•œ ๋ฐฉ๋ฒ•์—์„œ (๋ฌธ๋ฒ•)์œผ๋กœ ๋ฐ”๋€Œ์—ˆ๋‹ค๋Š” ์ฐจ์ด๊ฐ€ ์žˆ๋‹ค.
  6. ๋‹ค์ด๋‚ด๋ฏน ์ž„ํฌํŠธ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ (CJS)์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ณ , (ES ๋ชจ๋“ˆ)์—์„œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. (ES ๋ชจ๋“ˆ)์—์„œ ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋ ค๋ฉด (import)๋Œ€์‹  (import())๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
  7. ์ด๋ฒคํŠธ ๋ฃจํ”„์—์„œ, (process.nextTick(์ฝœ๋ฐฑ))๊ณผ (resolve๋œ Promise)๋Š” ๋‹ค๋ฅธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋“ค๋ณด๋‹ค ์šฐ์„ ์œผ๋กœ ์ฒ˜๋ฆฌ๋œ๋‹ค.

์ฝ”๋“œ ๋ฌธ์ œ

 

1. ๋‹ค์Œ CommonJS ๋ชจ๋“ˆ์„ EMCAScript ๋ชจ๋“ˆ ํ˜•์‹์œผ๋กœ ๋ณ€๊ฒฝํ•˜์„ธ์š”.

 

// var.js

const odd = 'CommonJS ํ™€์ˆ˜์ž…๋‹ˆ๋‹ค';
const even = 'CommonJS ์ง์ˆ˜์ž…๋‹ˆ๋‹ค';

module.exports = {
    odd,
    even,
};
// 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.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'));

 

 

2. ๋‹ค์Œ ์ฝ”๋“œ์˜ ์ถœ๋ ฅ์„ ์˜ˆ์ƒํ•˜์„ธ์š”.

// 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);
๋”๋ณด๊ธฐ

์ฆ‰์‹œ ์‹คํ–‰

1์ดˆ๋งˆ๋‹ค ์‹คํ–‰

1.5์ดˆ ํ›„ ์‹คํ–‰

1์ดˆ๋งˆ๋‹ค ์‹คํ–‰


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

Node.js #2 

Editor : ์œ ์ฆˆ

728x90

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