๐Ÿ”Ž๋…ธ๋“œ๊ธฐ๋Šฅ

์„œ๊ฐ€ํฌยท2021๋…„ 10์›” 7์ผ
0

Node.js

๋ชฉ๋ก ๋ณด๊ธฐ
2/15
post-thumbnail

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

REPL

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด๋ผ์„œ ์ฆ‰์„์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Œ

  • REPL์ด๋ผ๋Š” ์ฝ˜์†” ์ œ๊ณต
  • R(Read), E(Evaluate), P(Print), L(Loop)
  • ์œˆ๋„์—์„œ๋Š” ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ, ๋งฅ์ด๋‚˜ ๋ฆฌ๋ˆ…์Šค์—์„œ๋Š” ํ„ฐ๋ฏธ๋„์— node ์ž…๋ ฅ

ํ”„๋กฌํ”„ํŠธ๊ฐ€ > ๋ชจ์–‘์œผ๋กœ ๋ฐ”๋€Œ๋ฉด, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ž…๋ ฅ
์ž…๋ ฅํ•œ ๊ฐ’์˜ ๊ฒฐ๊ด๊ฐ’์ด ๋ฐ”๋กœ ์ถœ๋ ฅ๋จ.

  • ๊ฐ„๋‹จํ•œ ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ์šฉ๋„๋กœ ์ ํ•ฉ
  • ๊ธด ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ์—๋Š” ๋ถ€์ ํ•ฉ

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

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ํ†ต์งธ๋กœ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•

  • ์•„๋ฌด ํด๋”(๋””๋ ‰ํ„ฐ๋ฆฌ)์—์„œ helloWorld.js๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž
  • node [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ]๋กœ ์‹คํ–‰
  • ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ’์ด ์ถœ๋ ฅ๋จ

๐Ÿ”ปhelloworld.js

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

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

helloWorld();

โœ”๊ฒฐ๊ณผ

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

1. ๋ชจ๋“ˆ

๋…ธ๋“œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

  • ๋ชจ๋“ˆ: ํŠน์ •ํ•œ ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋“ค์˜ ์ง‘ํ•ฉ
  • ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค๋ฉด ์—ฌ๋Ÿฌ ํ”„๋กœ๊ทธ๋žจ์—์„œ ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ

2. ๋ชจ๋“ˆ ๋งŒ๋“ค์–ด๋ณด๊ธฐ

๊ฐ™์€ ํด๋” ๋‚ด์— var.js, func.js, index.js ๋งŒ๋“ค๊ธฐ

  • ํŒŒ์ผ ๋์— module.exports๋กœ ๋ชจ๋“ˆ๋กœ ๋งŒ๋“ค ๊ฐ’์„ ์ง€์ •
  • ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ require(ํŒŒ์ผ ๊ฒฝ๋กœ)๋กœ ๊ทธ ๋ชจ๋“ˆ์˜ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Œ

๐Ÿ”ปvar.js

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

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'));

3. ํŒŒ์ผ ๊ฐ„์˜ ๋ชจ๋“ˆ ๊ด€๊ณ„

node index๋กœ ์‹คํ–‰

  • const { odd, even } ๋ถ€๋ถ„์€ module.exports๋ฅผ ๊ตฌ์กฐ๋ถ„ํ•ด ํ• ๋‹นํ•œ ๊ฒƒ์ž„

4. ES2015 ๋ชจ๋“ˆ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ž์ฒด ๋ชจ๋“ˆ ์‹œ์Šคํ…œ ๋ฌธ๋ฒ•์ด ์ƒ๊น€

  • ์•„์ง ๋…ธ๋“œ์—์„œ์˜ ์ง€์›์€ ์™„๋ฒฝํ•˜์ง€ ์•Š์Œ. mjs ํ™•์žฅ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ.
  • ํฌ๊ฒŒ๋Š” require ๋Œ€์‹  import, module.exports ๋Œ€์‹  export default๋ฅผ ์“ฐ๋Š” ๊ฒƒ์œผ๋กœ ๋ฐ”๋€œ

๐Ÿ”ปfunc.mjs

import { odd, even } from './var';

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

export default checkOddOrEven;

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

1. global

๋…ธ๋“œ์˜ ์ „์—ญ ๊ฐ์ฒด

  • ๋ธŒ๋ผ์šฐ์ €์˜ window๊ฐ™์€ ์—ญํ• 
  • ๋ชจ๋“  ํŒŒ์ผ์—์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • window์ฒ˜๋Ÿผ ์ƒ๋žต๋„ ๊ฐ€๋Šฅ(console, require๋„ global์˜ ์†์„ฑ)

โœ”๊ฒฐ๊ณผ

2. global ์†์„ฑ ๊ณต์œ 

global ์†์„ฑ์— ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋ฉด ๋‹ค๋ฅธ ํŒŒ์ผ์—์„œ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐Ÿ”ปglobalA.js

module.exports = () => global.message;

๐Ÿ”ปglobalB.js

const A = require('./globalA');

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

โœ”๊ฒฐ๊ณผ

3. console ๊ฐ์ฒด

๋ธŒ๋ผ์šฐ์ €์˜ console ๊ฐ์ฒด์™€ ๋งค์šฐ ์œ ์‚ฌ

  • console.time, console.timeEnd: ์‹œ๊ฐ„ ๋กœ๊น…
  • console.error: ์—๋Ÿฌ ๋กœ๊น…
  • console.log: ํ‰๋ฒ”ํ•œ ๋กœ๊ทธ
  • console.dir: ๊ฐ์ฒด ๋กœ๊น…
  • console.trace: ํ˜ธ์ถœ์Šคํƒ ๋กœ๊น…

๐Ÿ”ปconsole.js

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('์ „์ฒด์‹œ๊ฐ„');

4. console ์˜ˆ์ œ ์‹คํ–‰ํ•˜๊ธฐ

node console๋กœ ์‹คํ–‰

5. ํƒ€์ด๋จธ ๋ฉ”์„œ๋“œ

set ๋ฉ”์„œ๋“œ์— clear ๋ฉ”์„œ๋“œ๊ฐ€ ๋Œ€์‘๋จ
set ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ๊ฐ’(์•„์ด๋””)์„ clear ๋ฉ”์„œ๋“œ์— ๋„ฃ์–ด ์ทจ์†Œ
setTimeout(์ฝœ๋ฐฑ ํ•จ์ˆ˜, ๋ฐ€๋ฆฌ์ดˆ): ์ฃผ์–ด์ง„ ๋ฐ€๋ฆฌ์ดˆ(1000๋ถ„์˜ 1์ดˆ) ์ดํ›„์— ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
setInterval(์ฝœ๋ฐฑ ํ•จ์ˆ˜, ๋ฐ€๋ฆฌ์ดˆ): ์ฃผ์–ด์ง„ ๋ฐ€๋ฆฌ์ดˆ๋งˆ๋‹ค ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
setImmediate(์ฝœ๋ฐฑ ํ•จ์ˆ˜): ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
clearTimeout(์•„์ด๋””): setTimeout์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.
clearInterval(์•„์ด๋””): setInterval์„ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.
clearImmediate(์•„์ด๋””): setImmediate๋ฅผ ์ทจ์†Œํ•ฉ๋‹ˆ๋‹ค.

6.ํƒ€์ด๋จธ ์˜ˆ์ œ

setTimeout(์ฝœ๋ฐฑ, 0)๋ณด๋‹ค setImmediate ๊ถŒ์žฅ

๐Ÿ”ป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);

โœ”๊ฒฐ๊ณผ

6. ํƒ€์ด๋จธ ์˜ˆ์ œ ๊ฒฐ๊ณผ


7. filename, dirname

filename: ํ˜„์žฌ ํŒŒ์ผ ๊ฒฝ๋กœ
dirname: ํ˜„์žฌ ํด๋”(๋””๋ ‰ํ„ฐ๋ฆฌ) ๊ฒฝ๋กœ

๐Ÿ”ปfilename.js

console.log(__filename);
console.log(__dirname);

โœ”๊ฒฐ๊ณผ

8. module, exports

module.exports ์™ธ์—๋„ exports๋กœ ๋ชจ๋“ˆ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

  • ๋ชจ๋“ˆ ์˜ˆ์ œ์˜ var.js๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊พผ ํ›„ ์‹คํ–‰
  • ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•จ
  • ๋™์ผํ•œ ์ด์œ ๋Š” module.exports์™€ exports๊ฐ€ ์ฐธ์กฐ ๊ด€๊ณ„์ด๊ธฐ ๋•Œ๋ฌธ
  • exports์— ๊ฐ์ฒด์˜ ์†์„ฑ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฐ’์„ ๋Œ€์ž…ํ•˜๋ฉด ์ฐธ์กฐ ๊ด€๊ณ„๊ฐ€ ๊นจ์ง

๐Ÿ”ปvar.js

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

โœ”๊ฒฐ๊ณผ

9. this

๋…ธ๋“œ์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฃผ์˜์ ์ด ์žˆ์Œ

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

๐Ÿ”ปthis.js

console.log(this);
console.log(this === module.exports);
console.log(this === exports);

function whatIsThis() {
  console.log('function', this === exports, this === global);
}
whatIsThis();

โœ”๊ฒฐ๊ณผ

10. require์˜ ํŠน์„ฑ

๋ช‡ ๊ฐ€์ง€ ์•Œ์•„๋‘˜ ๋งŒํ•œ ์†์„ฑ์ด ์žˆ์Œ

  • require๊ฐ€ ์ œ์ผ ์œ„์— ์˜ฌ ํ•„์š”๋Š” ์—†์Œ
  • require.cache์— ํ•œ ๋ฒˆ requireํ•œ ๋ชจ๋“ˆ์— ๋Œ€ํ•œ ์บ์Š ์ •๋ณด๊ฐ€ ๋“ค์–ด์žˆ์Œ.
  • require.main์€ ๋…ธ๋“œ ์‹คํ–‰ ์‹œ ์ฒซ ๋ชจ๋“ˆ์„ ๊ฐ€๋ฆฌํ‚ด.

๐Ÿ”ปrequire.js

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);

11. ์ˆœํ™˜์ฐธ์กฐ

๋‘ ๊ฐœ์˜ ๋ชจ๋“ˆ์ด ์„œ๋กœ๋ฅผ requireํ•˜๋Š” ์ƒํ™ฉ์„ ์กฐ์‹ฌํ•ด์•ผ ํ•จ

  • Dep1์ด dep2๋ฅผ requireํ•˜๊ณ , dep2๊ฐ€ dep1์„ require ํ•จ.
  • Dep1์˜ module.exports๊ฐ€ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋นˆ ๊ฐ์ฒด๊ฐ€ ๋จ(๋ฌดํ•œ ๋ฐ˜๋ณต์„ ๋ง‰๊ธฐ ์œ„ํ•ด ์˜๋„๋จ)
  • ์ˆœํ™˜์ฐธ์กฐํ•˜๋Š” ์ƒํ™ฉ์ด ๋‚˜์˜ค์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๊ฒŒ ์ข‹์Œ.

๐Ÿ”ป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();

โœ”๊ฒฐ๊ณผ

12. process

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

  • ์ปดํ“จํ„ฐ๋งˆ๋‹ค ์ถœ๋ ฅ๊ฐ’์ด PPT์™€ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ์Œ

13. process.env

์‹œ์Šคํ…œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด

  • ๋น„๋ฐ€ํ‚ค(๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋น„๋ฐ€๋ฒˆํ˜ธ, ์„œ๋“œํŒŒํ‹ฐ ์•ฑ ํ‚ค ๋“ฑ)๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ์šฉ๋„๋กœ๋„ ์“ฐ์ž„
  • ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋Š” process.env๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
const secretId= process.env.SECRET_ID;
const secretCode= process.env.SECRET_CODE;
  • ์ผ๋ถ€ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋Š” ๋…ธ๋“œ ์‹คํ–‰ ์‹œ ์˜ํ–ฅ์„ ๋ฏธ์นจ
  • ์˜ˆ์‹œ) NODE_OPTIONS(๋…ธ๋“œ ์‹คํ–‰ ์˜ต์…˜), UV_THREADPOOL_SIZE(์Šค๋ ˆ๋“œํ’€ ๊ฐœ์ˆ˜)
    max-old-space-size๋Š” ๋…ธ๋“œ๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ง€์ •ํ•˜๋Š” ์˜ต์…˜
NODE_OPTIONS=--max-lod-space-size=8192
UV_THREADPOOL_SIZE=8

14. process.nextTick(์ฝœ๋ฐฑ)

์ด๋ฒคํŠธ ๋ฃจํ”„๊ฐ€ ๋‹ค๋ฅธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋“ค๋ณด๋‹ค nextTick์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•จ

  • ๋„ˆ๋ฌด ๋‚จ์šฉํ•˜๋ฉด ๋‹ค๋ฅธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋“ค ์‹คํ–‰์ด ๋Šฆ์–ด์ง
  • ๋น„์Šทํ•œ ๊ฒฝ์šฐ๋กœ promise๊ฐ€ ์žˆ์Œ(nextTick์ฒ˜๋Ÿผ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Œ)
  • ์•„๋ž˜ ์˜ˆ์ œ์—์„œ setImmediate, setTimeout๋ณด๋‹ค promise์™€ nextTick์ด ๋จผ์ € ์‹คํ–‰๋จ

๐Ÿ”ปnextTick.js

setImmediate(() => {
  console.log('immediate');
});
process.nextTick(() => {
  console.log('nextTick');
});
setTimeout(() => {
  console.log('timeout');
}, 0);
Promise.resolve().then(() => console.log('promise'));

โœ”๊ฒฐ๊ณผ

15. process.exit(์ฝ”๋“œ)

ํ˜„์žฌ์˜ ํ”„๋กœ์„ธ์Šค๋ฅผ ๋ฉˆ์ถค

  • ์ฝ”๋“œ๊ฐ€ ์—†๊ฑฐ๋‚˜ 0์ด๋ฉด ์ •์ƒ ์ข…๋ฃŒ
  • ์ด์™ธ์˜ ์ฝ”๋“œ๋Š” ๋น„์ •์ƒ ์ข…๋ฃŒ๋ฅผ ์˜๋ฏธํ•จ

๐Ÿ”ปexit.js

let i = 1;
setInterval(() => {
  if (i === 5) {
    console.log('์ข…๋ฃŒ!');
    process.exit();
  }
  console.log(i);
  i += 1;
}, 1000);

โœ”๊ฒฐ๊ณผ

๋…ธ๋“œ ๋‚ด์žฅ ๋ชจ๋“ˆ ์‚ฌ์šฉํ•˜๊ธฐ

1. os

์šด์˜์ฒด์ œ์˜ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์Œ

  • ๋ชจ๋“ˆ์€ require๋กœ ๊ฐ€์ ธ์˜ด(๋‚ด์žฅ ๋ชจ๋“ˆ์ด๋ผ ๊ฒฝ๋กœ ๋Œ€์‹  ์ด๋ฆ„๋งŒ ์ ์–ด์ค˜๋„ ๋จ)![]

๐Ÿ”ปos.js

const os = require('os');

console.log('์šด์˜์ฒด์ œ ์ •๋ณด---------------------------------');
console.log('os.arch():', os.arch());
console.log('os.platform():', os.platform());
console.log('os.type():', os.type());
console.log('os.uptime():', os.uptime());
console.log('os.hostname():', os.hostname());
console.log('os.release():', os.release());

console.log('๊ฒฝ๋กœ------------------------------------------');
console.log('os.homedir():', os.homedir());
console.log('os.tmpdir():', os.tmpdir());

console.log('cpu ์ •๋ณด--------------------------------------');
console.log('os.cpus():', os.cpus());
console.log('os.cpus().length:', os.cpus().length);

console.log('๋ฉ”๋ชจ๋ฆฌ ์ •๋ณด-----------------------------------');
console.log('os.freemem():', os.freemem());
console.log('os.totalmem():', os.totalmem());

โœ”๊ฒฐ๊ณผ

2. os ๋ชจ๋“ˆ ๋ฉ”์„œ๋“œ

os.arch(): process.arch์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
os.platform(): process.platform๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
os.type(): ์šด์˜์ฒด์ œ์˜ ์ข…๋ฅ˜๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.uptime(): ์šด์˜์ฒด์ œ ๋ถ€ํŒ… ์ดํ›„ ํ๋ฅธ ์‹œ๊ฐ„(์ดˆ)์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. process.uptime()์€ ๋…ธ๋“œ์˜ ์‹คํ–‰ ์‹œ๊ฐ„์ด์—ˆ์Šต๋‹ˆ๋‹ค.
os.hostname(): ์ปดํ“จํ„ฐ์˜ ์ด๋ฆ„์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.release(): ์šด์˜์ฒด์ œ์˜ ๋ฒ„์ „์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.homedir(): ํ™ˆ ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.tmpdir(): ์ž„์‹œ ํŒŒ์ผ ์ €์žฅ ๊ฒฝ๋กœ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.cpus(): ์ปดํ“จํ„ฐ์˜ ์ฝ”์–ด ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.freemem(): ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉ”๋ชจ๋ฆฌ(RAM)๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
os.totalmem(): ์ „์ฒด ๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

3. path

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

  • ์šด์˜์ฒด์ œ๋ณ„๋กœ ๊ฒฝ๋กœ ๊ตฌ๋ถ„์ž๊ฐ€ ๋‹ค๋ฆ„(Windows: โ€˜\โ€™, POSIX: โ€˜/โ€™)

๐Ÿ”ป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\Seokahi\Desktop\๋…ธ๋“œ',
  name: 'path',
  ext: '.js',
}));
console.log('path.normalize():', path.normalize('C:\Users\Seokahi\Desktop\๋…ธ๋“œ\path.js'));
console.log('------------------------------');
console.log('path.isAbsolute(C:\\):', path.isAbsolute('C:\\'));
console.log('path.isAbsolute(./home):', path.isAbsolute('./home'));
console.log('------------------------------');
console.log('path.relative():', path.relative('C:\Users\Seokahi\Desktop\๋…ธ๋“œ\path.js', 'C:\\'));
console.log('path.join():', path.join(__dirname, '..', '..', '/users', '.', '/๋…ธ๋“œ'));
console.log('path.resolve():', path.resolve(__dirname, '..', 'users', '.', '/๋…ธ๋“œ'));

โœ”๊ฒฐ๊ณผ

4. path ๋ชจ๋“ˆ ๋ฉ”์„œ๋“œ

path.sep: ๊ฒฝ๋กœ์˜ ๊ตฌ๋ถ„์ž์ž…๋‹ˆ๋‹ค. Windows๋Š” \, POSIX๋Š” /์ž…๋‹ˆ๋‹ค.
path.delimiter: ํ™˜๊ฒฝ ๋ณ€์ˆ˜์˜ ๊ตฌ๋ถ„์ž์ž…๋‹ˆ๋‹ค. process.env.PATH๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฒฝ๋กœ๊ฐ€ ์ด ๊ตฌ๋ถ„์ž๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. Windows๋Š” ์„ธ๋ฏธ์ฝœ๋ก (;)์ด๊ณ  POSIX๋Š” ์ฝœ๋ก (:)์ž…๋‹ˆ๋‹ค.
path.dirname(๊ฒฝ๋กœ): ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ํด๋” ๊ฒฝ๋กœ๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
path.extname(๊ฒฝ๋กœ): ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.
path.basename(๊ฒฝ๋กœ, ํ™•์žฅ์ž): ํŒŒ์ผ์˜ ์ด๋ฆ„(ํ™•์žฅ์ž ํฌํ•จ)์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ํŒŒ์ผ์˜ ์ด๋ฆ„๋งŒ ํ‘œ์‹œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด basename์˜ ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ํŒŒ์ผ์˜ ํ™•์žฅ์ž๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
path.parse(๊ฒฝ๋กœ): ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ root, dir, base, ext, name์œผ๋กœ ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
path.format(๊ฐ์ฒด): path.parse()ํ•œ ๊ฐ์ฒด๋ฅผ ํŒŒ์ผ ๊ฒฝ๋กœ๋กœ ํ•ฉ์นฉ๋‹ˆ๋‹ค.
path.normalize(๊ฒฝ๋กœ): /๋‚˜ \๋ฅผ ์‹ค์ˆ˜๋กœ ์—ฌ๋Ÿฌ ๋ฒˆ ์‚ฌ์šฉํ–ˆ๊ฑฐ๋‚˜ ํ˜ผ์šฉํ–ˆ์„ ๋•Œ ์ •์ƒ์ ์ธ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜ํ•ด์ค๋‹ˆ๋‹ค.
path.isAbsolute(๊ฒฝ๋กœ): ํŒŒ์ผ์˜ ๊ฒฝ๋กœ๊ฐ€ ์ ˆ๋Œ€๊ฒฝ๋กœ์ธ์ง€ ์ƒ๋Œ€๊ฒฝ๋กœ์ธ์ง€ true๋‚˜ false๋กœ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.
path.relative(๊ธฐ์ค€๊ฒฝ๋กœ, ๋น„๊ต๊ฒฝ๋กœ): ๊ฒฝ๋กœ๋ฅผ ๋‘ ๊ฐœ ๋„ฃ์œผ๋ฉด ์ฒซ ๋ฒˆ์งธ ๊ฒฝ๋กœ์—์„œ ๋‘ ๋ฒˆ์งธ ๊ฒฝ๋กœ๋กœ ๊ฐ€๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.
path.join(๊ฒฝ๋กœ, .. .): ์—ฌ๋Ÿฌ ์ธ์ž๋ฅผ ๋„ฃ์œผ๋ฉด ํ•˜๋‚˜์˜ ๊ฒฝ๋กœ๋กœ ํ•ฉ์ณ์ค๋‹ˆ๋‹ค. ์ƒ๋Œ€๊ฒฝ๋กœ์ธ ..(๋ถ€๋ชจ ๋””๋ ‰ํ„ฐ๋ฆฌ)๊ณผ .(ํ˜„ ์œ„์น˜)๋„ ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.
path.resolve(๊ฒฝ๋กœ, .. .): path.join()๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฐจ์ด์ ์€ ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” Note์—์„œ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

5. ์•Œ์•„๋‘ฌ์•ผํ•  path ๊ด€๋ จ ์ •๋ณด

join๊ณผ resolve์˜ ์ฐจ์ด: resolve๋Š” /๋ฅผ ์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ์ฒ˜๋ฆฌ, join์€ ์ƒ๋Œ€๊ฒฝ๋กœ๋กœ ์ฒ˜๋ฆฌ

  • ์ƒ๋Œ€ ๊ฒฝ๋กœ: ํ˜„์žฌ ํŒŒ์ผ ๊ธฐ์ค€. ๊ฐ™์€ ๊ฒฝ๋กœ๋ฉด ์  ํ•˜๋‚˜(.), ํ•œ ๋‹จ๊ณ„ ์ƒ์œ„ ๊ฒฝ๋กœ๋ฉด ์  ๋‘ ๊ฐœ(..)

\์™€ \ ์ฐจ์ด: \๋Š” ์œˆ๋„ ๊ฒฝ๋กœ ๊ตฌ๋ถ„์ž, \๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฌธ์ž์—ด ์•ˆ์—์„œ ์‚ฌ์šฉ(\๊ฐ€ ํŠน์ˆ˜๋ฌธ์ž๋ผ \๋กœ ์ด์Šค์ผ€์ดํ”„ ํ•ด์ค€ ๊ฒƒ)
์œˆ๋„์—์„œ POSIX path๋ฅผ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด: path.posix ๊ฐ์ฒด ์‚ฌ์šฉ

  • POSIX์—์„œ ์œˆ๋„ path๋ฅผ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด: path.win32 ๊ฐ์ฒด ์‚ฌ์šฉ

6. url ๋ชจ๋“ˆ

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

  • url ์ฒ˜๋ฆฌ์— ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ์Œ(๊ธฐ์กด ๋…ธ๋“œ ๋ฐฉ์‹ vs WHATWG ๋ฐฉ์‹
  • ์•„๋ž˜ ๊ทธ๋ฆผ์—์„œ ๊ฐ€์šด๋ฐ ์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์œ„์ชฝ์€ ๊ธฐ์กด ๋…ธ๋“œ ๋ฐฉ์‹, ์•„๋ž˜์ชฝ์€ WHATWG ๋ฐฉ์‹

7. url ๋ชจ๋“ˆ ์˜ˆ์ œ

๐Ÿ”ปurl.js

const url = require('url');

const { URL } = url;
const myURL = new URL('http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor');
console.log('new URL():', myURL);
console.log('url.format():', url.format(myURL));
console.log('------------------------------');
const parsedUrl = url.parse('http://www.gilbut.co.kr/book/bookList.aspx?sercate1=001001000#anchor');
console.log('url.parse():', parsedUrl);
console.log('url.format():', url.format(parsedUrl));

โœ”๊ฒฐ๊ณผ

8. url ๋ชจ๋“ˆ ์˜ˆ์ œ ๊ฒฐ๊ณผ

9. url ๋ชจ๋“ˆ ๋ฉ”์„œ๋“œ

๊ธฐ์กด ๋…ธ๋“œ ๋ฐฉ์‹ ๋ฉ”์„œ๋“œ

  • url.parse(์ฃผ์†Œ): ์ฃผ์†Œ๋ฅผ ๋ถ„ํ•ดํ•ฉ๋‹ˆ๋‹ค. WHATWG ๋ฐฉ์‹๊ณผ ๋น„๊ตํ•˜๋ฉด username๊ณผ password๋Œ€์‹  auth ์†์„ฑ์ด ์žˆ๊ณ , searchParams ๋Œ€์‹  query๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
  • url.format(๊ฐ์ฒด): WHATWG ๋ฐฉ์‹์˜ url๊ณผ ๊ธฐ์กด ๋…ธ๋“œ์˜ url ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ถ„ํ•ด๋˜์—ˆ๋˜ url ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ์›๋ž˜ ์ƒํƒœ๋กœ ์กฐ๋ฆฝํ•ฉ๋‹ˆ๋‹ค.

10. searchParams

WHATWG ๋ฐฉ์‹์—์„œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง(search) ๋ถ€๋ถ„ ์ฒ˜๋ฆฌ๋ฅผ ๋„์™€์ฃผ๋Š” ๊ฐ์ฒด
?page=3&limit=10&category=nodejs&category=javascript ๋ถ€๋ถ„

๐Ÿ”ปsearchParams.js

const { URL } = require('url');

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();

getAll(ํ‚ค): ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ๊ฐ’๋“ค์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. category ํ‚ค์—๋Š” ๋‘ ๊ฐ€์ง€ ๊ฐ’, ์ฆ‰ nodejs์™€ javascript์˜ ๊ฐ’์ด ๋“ค์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
get(ํ‚ค): ํ‚ค์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ๊ฐ’๋งŒ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
has(ํ‚ค): ํ•ด๋‹น ํ‚ค๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.
keys(): searchParams์˜ ๋ชจ๋“  ํ‚ค๋ฅผ ๋ฐ˜๋ณต๊ธฐ(iterator, ES2015 ๋ฌธ๋ฒ•) ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
values(): searchParams์˜ ๋ชจ๋“  ๊ฐ’์„ ๋ฐ˜๋ณต๊ธฐ ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
append(ํ‚ค, ๊ฐ’): ํ•ด๋‹น ํ‚ค๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ™์€ ํ‚ค์˜ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์œ ์ง€ํ•˜๊ณ  ํ•˜๋‚˜ ๋” ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
set(ํ‚ค, ๊ฐ’): append์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๊ฐ™์€ ํ‚ค์˜ ๊ฐ’๋“ค์„ ๋ชจ๋‘ ์ง€์šฐ๊ณ  ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
delete(ํ‚ค): ํ•ด๋‹น ํ‚ค๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
toString(): ์กฐ์ž‘ํ•œ searchParams ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด ๋ฌธ์ž์—ด์„ search์— ๋Œ€์ž…ํ•˜๋ฉด ์ฃผ์†Œ ๊ฐ์ฒด์— ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค.

โœ”๊ฒฐ๊ณผ

12. querystring

๊ธฐ์กด ๋…ธ๋“œ ๋ฐฉ์‹์—์„œ๋Š” url querystring์„ querystring ๋ชจ๋“ˆ๋กœ ์ฒ˜๋ฆฌ

  • querystring.parse(์ฟผ๋ฆฌ): url์˜ query ๋ถ€๋ถ„์„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด๋กœ ๋ถ„ํ•ดํ•ด์ค๋‹ˆ๋‹ค.
  • querystring.stringify(๊ฐ์ฒด): ๋ถ„ํ•ด๋œ query ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋‹ค์‹œ ์กฐ๋ฆฝํ•ด์ค๋‹ˆ๋‹ค.

querystring.js

const url = require('url');
const querystring = require('querystring');

const parsedUrl = url.parse('http://www.gilbut.co.kr/?page=3&limit=10&category=nodejs&category=javascript');
const query = querystring.parse(parsedUrl.query);
console.log('querystring.parse():', query);
console.log('querystring.stringify():', querystring.stringify(query));

โœ”๊ฒฐ๊ณผ

13. ๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™”(crypto)

์•”ํ˜ธํ™”๋Š” ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๋ณตํ˜ธํ™”๋Š” ๋ถˆ๊ฐ€๋Šฅ

  • ์•”ํ˜ธํ™”: ํ‰๋ฌธ์„ ์•”ํ˜ธ๋กœ ๋งŒ๋“ฆ
  • ๋ณตํ˜ธํ™”: ์•”ํ˜ธ๋ฅผ ํ‰๋ฌธ์œผ๋กœ ํ•ด๋…

๋‹จ๋ฐฉํ–ฅ ์•”ํ˜ธํ™”์˜ ๋Œ€ํ‘œ ์ฃผ์ž๋Š” ํ•ด์‹œ ๊ธฐ๋ฒ•

  • ๋ฌธ์ž์—ด์„ ๊ณ ์ •๋œ ๊ธธ์ด์˜ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ์‹
  • abcdefgh ๋ฌธ์ž์—ด -> qvew

14. Hash ์‚ฌ์šฉํ•˜๊ธฐ(sha512)

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

  • md5, sha1, sha256, sha512 ๋“ฑ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, md5์™€ sha1์€ ์ด๋ฏธ ์ทจ์•ฝ์ ์ด ๋ฐœ๊ฒฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • ํ˜„์žฌ๋Š” sha512 ์ •๋„๋กœ ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, ๋‚˜์ค‘์— sha512๋งˆ์ €๋„ ์ทจ์•ฝํ•ด์ง€๋ฉด ๋” ๊ฐ•ํ™”๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    update(๋ฌธ์ž์—ด): ๋ณ€ํ™˜ํ•  ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
    digest(์ธ์ฝ”๋”ฉ): ์ธ์ฝ”๋”ฉํ•  ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
    base64, hex, latin1์ด ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ๊ทธ์ค‘ base64๊ฐ€ ๊ฒฐ๊ณผ ๋ฌธ์ž์—ด์ด ๊ฐ€์žฅ ์งง์•„ ์• ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฒฐ๊ณผ๋ฌผ๋กœ ๋ณ€ํ™˜๋œ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

14. pbkdf2

์ปดํ“จํ„ฐ์˜ ๋ฐœ๋‹ฌ๋กœ ๊ธฐ์กด ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์œ„ํ˜‘๋ฐ›๊ณ  ์žˆ์Œ

  • sha512๊ฐ€ ์ทจ์•ฝํ•ด์ง€๋ฉด sha3์œผ๋กœ ๋„˜์–ด๊ฐ€์•ผํ•จ
  • ํ˜„์žฌ๋Š” pbkdf2๋‚˜, bcrypt, scrypt ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์•”ํ˜ธํ™”
  • Node๋Š” pbkdf2์™€ scrypt ์ง€์›

15. pbkdf2 ์˜ˆ์ œ

์ปดํ“จํ„ฐ์˜ ๋ฐœ๋‹ฌ๋กœ ๊ธฐ์กด ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์œ„ํ˜‘๋ฐ›๊ณ  ์žˆ์Œ

  • crypto.randomBytes๋กœ 64๋ฐ”์ดํŠธ ๋ฌธ์ž์—ด ์ƒ์„ฑ -> salt ์—ญํ• 
  • pbkdf2 ์ธ์ˆ˜๋กœ ์ˆœ์„œ๋Œ€๋กœ ๋น„๋ฐ€๋ฒˆํ˜ธ, salt, ๋ฐ˜๋ณต ํšŸ์ˆ˜, ์ถœ๋ ฅ ๋ฐ”์ดํŠธ, ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • ๋ฐ˜๋ณต ํšŸ์ˆ˜๋ฅผ ์กฐ์ •ํ•ด ์•”ํ˜ธํ™”ํ•˜๋Š” ๋ฐ 1์ดˆ ์ •๋„ ๊ฑธ๋ฆฌ๊ฒŒ ๋งž์ถ”๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋จ

๐Ÿ”ปpbkdf2.js

const crypto = require('crypto');

crypto.randomBytes(64, (err, buf) => {
  const salt = buf.toString('base64');
  console.log('salt:', salt);
  crypto.pbkdf2('๋น„๋ฐ€๋ฒˆํ˜ธ', salt, 100000, 64, 'sha512', (err, key) => {
    console.log('password:', key.toString('base64'));
  });
});

โœ”๊ฒฐ๊ณผ

16. ์–‘๋ฐฉํ–ฅ ์•”ํ˜ธํ™”

๋Œ€์นญํ˜• ์•”ํ˜ธํ™”(์•”ํ˜ธ๋ฌธ ๋ณตํ˜ธํ™” ๊ฐ€๋Šฅ)

  • Key๊ฐ€ ์‚ฌ์šฉ๋จ
  • ์•”ํ˜ธํ™”ํ•  ๋•Œ์™€ ๋ณตํ˜ธํ™” ํ•  ๋•Œ ๊ฐ™์€ Key๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

๐Ÿ”ป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);

17. ์–‘๋ฐฉํ–ฅ ์•”ํ˜ธํ™” ๋ฉ”์„œ๋“œ

crypto.createCipheriv(์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ‚ค, iv): ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ‚ค, ์ดˆ๊ธฐํ™”๋ฒกํ„ฐ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ aes-256-cbc๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.
์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ชฉ๋ก์€ crypto.getCiphers()๋ฅผ ํ•˜๋ฉด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ‚ค๋Š” 32๋ฐ”์ดํŠธ, ์ดˆ๊ธฐํ™”๋ฒกํ„ฐ(iv)๋Š” 16๋ฐ”์ดํŠธ๋กœ ๊ณ ์ •์ž…๋‹ˆ๋‹ค.
cipher.update(๋ฌธ์ž์—ด, ์ธ์ฝ”๋”ฉ, ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ): ์•”ํ˜ธํ™”ํ•  ๋Œ€์ƒ๊ณผ ๋Œ€์ƒ์˜ ์ธ์ฝ”๋”ฉ, ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ์˜ ์ธ์ฝ”๋”ฉ์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
๋ณดํ†ต ๋ฌธ์ž์—ด์€ utf8 ์ธ์ฝ”๋”ฉ์„, ์•”ํ˜ธ๋Š” base64๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
cipher.final(์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ): ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋ฌผ์˜ ์ธ์ฝ”๋”ฉ์„ ๋„ฃ์–ด์ฃผ๋ฉด ์•”ํ˜ธํ™”๊ฐ€ ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค.
crypto.createDecipheriv(์•Œ๊ณ ๋ฆฌ์ฆ˜, ํ‚ค, iv): ๋ณตํ˜ธํ™”ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•”ํ˜ธํ™”ํ•  ๋•Œ ์‚ฌ์šฉํ–ˆ๋˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ‚ค, iv๋ฅผ ๊ทธ๋Œ€๋กœ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
decipher.update(๋ฌธ์ž์—ด, ์ธ์ฝ”๋”ฉ, ์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ): ์•”ํ˜ธํ™”๋œ ๋ฌธ์žฅ, ๊ทธ ๋ฌธ์žฅ์˜ ์ธ์ฝ”๋”ฉ, ๋ณตํ˜ธํ™”ํ•  ์ธ์ฝ”๋”ฉ์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.
createCipher์˜ update()์—์„œ utf8, base64 ์ˆœ์œผ๋กœ ๋„ฃ์—ˆ๋‹ค๋ฉด createDecipher์˜ update()์—์„œ๋Š” base64, utf8 ์ˆœ์œผ๋กœ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
decipher.final(์ถœ๋ ฅ ์ธ์ฝ”๋”ฉ): ๋ณตํ˜ธํ™” ๊ฒฐ๊ณผ๋ฌผ์˜ ์ธ์ฝ”๋”ฉ์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

18. util

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

  • deprecated์™€ promisify๊ฐ€ ์ž์ฃผ ์“ฐ์ž„

๐Ÿ”ปutil.js

const util = require('util');
const crypto = require('crypto');

const dontUseMe = util.deprecate((x, y) => {
  console.log(x + y);
}, 'dontUseMe ํ•จ์ˆ˜๋Š” deprecated๋˜์—ˆ์œผ๋‹ˆ ๋” ์ด์ƒ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”!');
dontUseMe(1, 2);

const randomBytesPromise = util.promisify(crypto.randomBytes);
randomBytesPromise(64)
  .then((buf) => {
    console.log(buf.toString('base64'));
  })
  .catch((error) => {
    console.error(error);
  });

โœ”๊ฒฐ๊ณผ

19. util์˜ ๋ฉ”์„œ๋“œ

util.deprecate: ํ•จ์ˆ˜๊ฐ€ deprecated ์ฒ˜๋ฆฌ๋˜์—ˆ์Œ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋„ฃ์€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๊ฒฝ๊ณ  ๋ฉ”์‹œ์ง€ ๋‚ด์šฉ์„ ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์กฐ๋งŒ๊ฐ„ ์‚ฌ๋ผ์ง€๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ๋  ๋•Œ ์•Œ๋ ค์ค„ ์ˆ˜ ์žˆ์–ด ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
    util.promisify: ์ฝœ๋ฐฑ ํŒจํ„ด์„ ํ”„๋กœ๋ฏธ์Šค ํŒจํ„ด์œผ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.
  • ๋ฐ”๊ฟ€ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ์ œ๊ณตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฐ”๊พธ์–ด๋‘๋ฉด async/await ํŒจํ„ด๊นŒ์ง€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด ์ข‹์Šต๋‹ˆ๋‹ค.
  • ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๋Š” util.callbackify๋„ ์žˆ์ง€๋งŒ ์ž์ฃผ ์‚ฌ์šฉ๋˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค.

20. worker_threads

๋…ธ๋“œ์—์„œ ๋ฉ€ํ‹ฐ ์Šค๋ ˆ๋“œ ๋ฐฉ์‹์œผ๋กœ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Œ.

  • isMainThread: ํ˜„์žฌ ์ฝ”๋“œ๊ฐ€ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋˜๋Š”์ง€, ์›Œ์ปค ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋˜๋Š”์ง€ ๊ตฌ๋ถ„
  • ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ๋Š” new Worker๋ฅผ ํ†ตํ•ด ํ˜„์žฌ ํŒŒ์ผ(__filename)์„ ์›Œ์ปค ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰์‹œํ‚ด
  • worker.postMessage๋กœ ๋ถ€๋ชจ์—์„œ ์›Œ์ปค๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋ƒ„
  • parentPort.on(โ€˜messageโ€™)๋กœ ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ , postMessage๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋ƒ„

๐Ÿ”ปworker_threads.js

const {
  Worker, isMainThread, parentPort,
} = require('worker_threads');

if (isMainThread) { // ๋ถ€๋ชจ์ผ ๋•Œ
  const worker = new Worker(__filename);
  worker.on('message', message => console.log('from worker', message));
  worker.on('exit', () => console.log('worker exit'));
  worker.postMessage('ping');
} else { // ์›Œ์ปค์ผ ๋•Œ
  parentPort.on('message', (value) => {
    console.log('from parent', value);
    parentPort.postMessage('pong');
    parentPort.close();
  });
}

โœ”๊ฒฐ๊ณผ

21. ์—ฌ๋Ÿฌ ์›Œ์ปค์Šค๋ ˆ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

new Worker ํ˜ธ์ถœํ•˜๋Š” ์ˆ˜๋งŒํผ ์›Œ์ปค ์Šค๋ ˆ๋“œ๊ฐ€ ์ƒ์„ฑ๋จ

๐Ÿ”ป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);
}

โœ”๊ฒฐ๊ณผ

22. ์†Œ์ˆ˜ ์ฐพ๊ธฐ ์˜ˆ์ œ

์›Œ์ปค ์Šค๋ ˆ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๋•Œ(์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ์ผ ๋•Œ)

๐Ÿ”ปprime-worker.js

const { Worker, isMainThread, parentPort, workerData } = require('worker_threads');

const min = 2;
let primes = [];

function findPrimes(start, range) {
  let isPrime = true;
  const end = start + range;
  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.ceil((max - min) / threadCount);
  let start = min;
  console.time('prime');
  for (let i = 0; i < threadCount - 1; i++) {
    const wStart = start;
    threads.add(new Worker(__filename, { workerData: { start: wStart, range } }));
    start += range;
  }
  threads.add(new Worker(__filename, { workerData: { start, range: range + ((max - min + 1) % threadCount) } }));
  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);
}

โœ”๊ฒฐ๊ณผ

23. child_process

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

  • ํ˜„์žฌ ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค ์™ธ์— ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์Šค๋ฅผ ๋„์›Œ์„œ ๋ช…๋ น์„ ์ˆ˜ํ–‰ํ•จ.
  • ๋ช…๋ น ํ”„๋กฌํ”„ํŠธ์˜ ๋ช…๋ น์–ด์ธ dir์„ ๋…ธ๋“œ๋ฅผ ํ†ตํ•ด ์‹คํ–‰(๋ฆฌ๋ˆ…์Šค๋ผ๋ฉด ls๋ฅผ ๋Œ€์‹  ์ ์„ ๊ฒƒ)

๐Ÿ”ปexec.js

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

var process = exec('dir');

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

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

24. child_process

ํŒŒ์ด์ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ํ•˜๊ธฐ
ํŒŒ์ด์ฌ3์ด ์„ค์น˜๋˜์–ด ์žˆ์–ด์•ผ ํ•จ.
๐Ÿ”ปtest.py

print('hello python')

๐Ÿ”ปspqwn.js

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

var 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());
}); // ์‹คํ–‰ ์—๋Ÿฌ

โœ”๊ฒฐ๊ณผ

25. ๊ธฐํƒ€ ๋ชจ๋“ˆ๋“ค

assert: ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์ด ์ œ๋Œ€๋กœ ๋™์ž‘ํ•˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
dns: ๋„๋ฉ”์ธ ์ด๋ฆ„์— ๋Œ€ํ•œ IP ์ฃผ์†Œ๋ฅผ ์–ป์–ด๋‚ด๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
net: HTTP๋ณด๋‹ค ๋กœ์šฐ ๋ ˆ๋ฒจ์ธ TCP๋‚˜ IPC ํ†ต์‹ ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
string_decoder: ๋ฒ„ํผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ๋Š” ๋ฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
tls: TLS์™€ SSL์— ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
tty: ํ„ฐ๋ฏธ๋„๊ณผ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
dgram: UDP์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
v8: V8 ์—”์ง„์— ์ง์ ‘ ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
vm: ๊ฐ€์ƒ ๋จธ์‹ ์— ์ง์ ‘ ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ผ ์‹œ์Šคํ…œ ์ ‘๊ทผํ•˜๊ธฐ

1. fs

ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์ ‘๊ทผํ•˜๋Š” ๋ชจ๋“ˆ

  • ํŒŒ์ผ/ํด๋” ์ƒ์„ฑ, ์‚ญ์ œ, ์ฝ๊ธฐ, ์“ฐ๊ธฐ ๊ฐ€๋Šฅ
  • ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ œํ•œ์ ์ด์—ˆ์œผ๋‚˜ ๋…ธ๋“œ๋Š” ๊ถŒํ•œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • ํŒŒ์ผ ์ฝ๊ธฐ ์˜ˆ์ œ(๊ฒฐ๊ณผ์˜ ๋ฒ„ํผ๋Š” ๋’ค์—์„œ ์„ค๋ช…ํ•จ)

๐Ÿ”ปreadme.txt

์ €๋ฅผ ์ฝ์–ด์ฃผ์„ธ์š”.

๐Ÿ”ปreadFile.js

const fs = require('fs');

fs.readFile('./readme.txt', (err, data) => {
  if (err) {
    throw err;
  }
  console.log(data);
  console.log(data.toString());
});

โœ”๊ฒฐ๊ณผ

2. fs ํ”„๋กœ๋ฏธ์Šค

์ฝœ๋ฐฑ ๋ฐฉ์‹ ๋Œ€์‹  ํ”„๋กœ๋ฏธ์Šค ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

  • require(โ€˜fsโ€™).promises
  • ์‚ฌ์šฉํ•˜๊ธฐ ํ›จ์”ฌ ๋” ํŽธํ•ด์„œ ํ”„๋กœ๋ฏธ์Šค ๋ฐฉ์‹์„ ์ถ”์ฒœํ•จ

๐Ÿ”ปreadfilepromise.js

const fs = require('fs').promises;

fs.readFile('./readme.txt')
  .then((data) => {
    console.log(data);
    console.log(data.toString());
  })
  .catch((err) => {
    console.error(err);
  });

3. fs๋กœ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

ํŒŒ์ผ์„ ๋งŒ๋“œ๋Š” ์˜ˆ์ œ

๐Ÿ”ปwriteFile.js

const fs = require('fs');

fs.writeFile('./writeme.txt', '๊ธ€์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค', (err) => {
  if (err) {
    throw err;
  }
  fs.readFile('./writeme.txt', (err, data) => {
    if (err) {
      throw err;
    }
    console.log(data.toString());
  });
});

โœ”๊ฒฐ๊ณผ

4. ๋™๊ธฐ ๋ฉ”์„œ๋“œ์™€ ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ

๋…ธ๋“œ๋Š” ๋Œ€๋ถ€๋ถ„์˜ ๋‚ด์žฅ ๋ชจ๋“ˆ ๋ฉ”์„œ๋“œ๋ฅผ ๋น„๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ

  • ๋น„๋™๊ธฐ๋Š” ์ฝ”๋“œ์˜ ์ˆœ์„œ์™€ ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ์˜๋ฏธ
  • ์ผ๋ถ€๋Š” ๋™๊ธฐ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์šฐ์ธก ์ฝ”๋“œ ์ฝ˜์†” ์˜ˆ์ธกํ•ด๋ณด๊ธฐ
๐Ÿ”ปreadme2.txt
์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์ฝ์–ด๋ณด์„ธ์š”.

๐Ÿ”ปasync.js

const fs = require('fs');

console.log('์‹œ์ž‘');
fs.readFile('./readme2.txt', (err, data) => {
  if (err) {
    throw err;
  }
  console.log('1๋ฒˆ', data.toString());
});
fs.readFile('./readme2.txt', (err, data) => {
  if (err) {
    throw err;
  }
  console.log('2๋ฒˆ', data.toString());
});
fs.readFile('./readme2.txt', (err, data) => {
  if (err) {
    throw err;
  }
  console.log('3๋ฒˆ', data.toString());
});
console.log('๋');

โœ”๊ฒฐ๊ณผ

5. ๋™๊ธฐ ๋ฉ”์„œ๋“œ์™€ ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ

์ด์ „ ์˜ˆ์ œ๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•ด๋ณด๊ธฐ

  • ๋งค ๋ฒˆ ์ˆœ์„œ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ์‹คํ–‰๋จ
  • ์ˆœ์„œ์— ๋งž๊ฒŒ ์‹คํ–‰ํ•˜๋ ค๋ฉด?

๋™๊ธฐ์™€ ๋น„๋™๊ธฐ: ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ž‘์—… ์™„๋ฃŒ ํ™•์ธ ์—ฌ๋ถ€
๋ธ”๋กœํ‚น๊ณผ ๋…ผ ๋ธ”๋กœํ‚น: ํ•จ์ˆ˜๊ฐ€ ๋ฐ”๋กœ return ๋˜๋Š”์ง€ ์—ฌ๋ถ€
๋…ธ๋“œ์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ ๋™๊ธฐ-๋ธ”๋กœํ‚น ๋ฐฉ์‹๊ณผ ๋น„๋™๊ธฐ-๋…ผ ๋ธ”๋กœํ‚น ๋ฐฉ์‹์ž„.

6. ๋™๊ธฐ ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ”ปsync.js

const fs = require('fs');

console.log('์‹œ์ž‘');
let data = fs.readFileSync('./readme2.txt');
console.log('1๋ฒˆ', data.toString());
data = fs.readFileSync('./readme2.txt');
console.log('2๋ฒˆ', data.toString());
data = fs.readFileSync('./readme2.txt');
console.log('3๋ฒˆ', data.toString());
console.log('๋');

โœ”๊ฒฐ๊ณผ

7. ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ๋กœ ์ˆœ์„œ ์œ ์ง€ํ•˜๊ธฐ

์ฝœ๋ฐฑ ํ˜•์‹ ์œ ์ง€

  • ์ฝ”๋“œ๊ฐ€ ์šฐ์ธก์œผ๋กœ ๋„ˆ๋ฌด ๋“ค์–ด๊ฐ€๋Š” ํ˜„์ƒ ๋ฐœ์ƒ(์ฝœ๋ฐฑ ํ—ฌ)

๐Ÿ”ปasyncOrder.js

const fs = require('fs');

console.log('์‹œ์ž‘');
fs.readFile('./readme2.txt', (err, data) => {
  if (err) {
    throw err;
  }
  console.log('1๋ฒˆ', data.toString());
  fs.readFile('./readme2.txt', (err, data) => {
    if (err) {
      throw err;
    }
    console.log('2๋ฒˆ', data.toString());
    fs.readFile('./readme2.txt', (err, data) => {
      if (err) {
        throw err;
      }
      console.log('3๋ฒˆ', data.toString());
      console.log('๋');
    });
  });
});

โœ”๊ฒฐ๊ณผ

8. ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ๋กœ ์ˆœ์„œ ์œ ์ง€ํ•˜๊ธฐ

ํ”„๋กœ๋ฏธ์Šค๋กœ ๊ทน๋ณต

๐Ÿ”ปasyncorderpromise.js

const fs = require('fs').promises;

console.log('์‹œ์ž‘');
fs.readFile('./readme2.txt')
  .then((data) => {
    console.log('1๋ฒˆ', data.toString());
    return fs.readFile('./readme2.txt');
  })
  .then((data) => {
    console.log('2๋ฒˆ', data.toString());
    return fs.readFile('./readme2.txt');
  })
  .then((data) => {
    console.log('3๋ฒˆ', data.toString());
    console.log('๋');
  })
  .catch((err) => {
    console.error(err);
  });

9. ๋ฒ„ํผ์™€ ์ŠคํŠธ๋ฆผ ์ดํ•ดํ•˜๊ธฐ

๋ฒ„ํผ: ์ผ์ •ํ•œ ํฌ๊ธฐ๋กœ ๋ชจ์•„๋‘๋Š” ๋ฐ์ดํ„ฐ

  • ์ผ์ •ํ•œ ํฌ๊ธฐ๊ฐ€ ๋˜๋ฉด ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌ
  • ๋ฒ„ํผ๋ง: ๋ฒ„ํผ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ฐฐ ๋•Œ๊นŒ์ง€ ๋ชจ์œผ๋Š” ์ž‘์—…
    ์ŠคํŠธ๋ฆผ: ๋ฐ์ดํ„ฐ์˜ ํ๋ฆ„
  • ์ผ์ •ํ•œ ํฌ๊ธฐ๋กœ ๋‚˜๋ˆ ์„œ ์—ฌ๋Ÿฌ ๋ฒˆ์— ๊ฑธ์ณ์„œ ์ฒ˜๋ฆฌ
  • ๋ฒ„ํผ(๋˜๋Š” ์ฒญํฌ)์˜ ํฌ๊ธฐ๋ฅผ ์ž‘๊ฒŒ ๋งŒ๋“ค์–ด์„œ ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌ
  • ์ŠคํŠธ๋ฆฌ๋ฐ: ์ผ์ •ํ•œ ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ง€์†์ ์œผ๋กœ ์ „๋‹ฌํ•˜๋Š” ์ž‘์—…

10. ๋ฒ„ํผ ์‚ฌ์šฉํ•˜๊ธฐ

๋…ธ๋“œ์—์„œ๋Š” Buffer ๊ฐ์ฒด ์‚ฌ์šฉ
๐Ÿ”ปbuffer.js

const buffer = Buffer.from('์ €๋ฅผ ๋ฒ„ํผ๋กœ ๋ฐ”๊ฟ”๋ณด์„ธ์š”');
console.log('from():', buffer);
console.log('length:', buffer.length);
console.log('toString():', buffer.toString());

const array = [Buffer.from('๋„์—„ '), Buffer.from('๋„์—„ '), Buffer.from('๋„์–ด์“ฐ๊ธฐ')];
const buffer2 = Buffer.concat(array);
console.log('concat():', buffer2.toString());

const buffer3 = Buffer.alloc(5);
console.log('alloc():', buffer3);

โœ”๊ฒฐ๊ณผ

11. Buffer์˜ ๋ฉ”์„œ๋“œ

๋…ธ๋“œ์—์„œ๋Š” Buffer ๊ฐ์ฒด ์‚ฌ์šฉ

  • from(๋ฌธ์ž์—ด): ๋ฌธ์ž์—ด์„ ๋ฒ„ํผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. length ์†์„ฑ์€ ๋ฒ„ํผ์˜ ํฌ๊ธฐ๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๋ฐ”์ดํŠธ ๋‹จ์œ„์ž…๋‹ˆ๋‹ค.
  • toString(๋ฒ„ํผ): ๋ฒ„ํผ๋ฅผ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ base64๋‚˜ hex๋ฅผ ์ธ์ž๋กœ ๋„ฃ์œผ๋ฉด ํ•ด๋‹น ์ธ์ฝ”๋”ฉ์œผ๋กœ๋„ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • concat(๋ฐฐ์—ด): ๋ฐฐ์—ด ์•ˆ์— ๋“  ๋ฒ„ํผ๋“ค์„ ํ•˜๋‚˜๋กœ ํ•ฉ์นฉ๋‹ˆ๋‹ค.
  • alloc(๋ฐ”์ดํŠธ): ๋นˆ ๋ฒ„ํผ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”์ดํŠธ๋ฅผ ์ธ์ž๋กœ ์ง€์ •ํ•ด์ฃผ๋ฉด ํ•ด๋‹น ํฌ๊ธฐ์˜ ๋ฒ„ํผ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

12. ํŒŒ์ผ ์ฝ๋Š” ์ŠคํŠธ๋ฆผ ์‚ฌ์šฉํ•˜๊ธฐ

fs.createReadStream

  • createReadStream์— ์ธ์ž๋กœ ํŒŒ์ผ ๊ฒฝ๋กœ์™€ ์˜ต์…˜ ๊ฐ์ฒด ์ „๋‹ฌ
  • highWaterMark ์˜ต์…˜์€ ๋ฒ„ํผ์˜ ํฌ๊ธฐ(๋ฐ”์ดํŠธ ๋‹จ์œ„, ๊ธฐ๋ณธ๊ฐ’ 64KB)
  • data(chunk ์ „๋‹ฌ), end(์ „๋‹ฌ ์™„๋ฃŒ), error(์—๋Ÿฌ ๋ฐœ์ƒ) ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์™€ ๊ฐ™์ด ์‚ฌ์šฉ

๐Ÿ”ปreadmetext3.txt

์ €๋Š” ์กฐ๊ธˆ์”ฉ ์กฐ๊ธˆ์”ฉ ๋‚˜๋ˆ ์„œ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ๋‚˜๋ˆ ์ง„ ์กฐ๊ฐ์„ chunk๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

๐Ÿ”ปcreatereadStream.js

const fs = require('fs');

const readStream = fs.createReadStream('./readme3.txt', { highWaterMark: 16 });
const data = [];

readStream.on('data', (chunk) => {
  data.push(chunk);
  console.log('data :', chunk, chunk.length);
});

readStream.on('end', () => {
  console.log('end :', Buffer.concat(data).toString());
});

readStream.on('error', (err) => {
  console.log('error :', err);
});

โœ”๊ฒฐ๊ณผ

13. ํŒŒ์ผ ์“ฐ๋Š” ์ŠคํŠธ๋ฆผ ์‚ฌ์šฉํ•˜๊ธฐ

๐Ÿ”ปfs.createWriteStream

  • createReadStream์— ์ธ์ž๋กœ ํŒŒ์ผ ๊ฒฝ๋กœ ์ „๋‹ฌ
  • write๋กœ chunk ์ž…๋ ฅ, end๋กœ ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ
  • ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ ์‹œ finish ์ด๋ฒคํŠธ ๋ฐœ์ƒ

createwritestream.js

const fs = require('fs');

const writeStream = fs.createWriteStream('./writeme2.txt');
writeStream.on('finish', () => {
  console.log('ํŒŒ์ผ ์“ฐ๊ธฐ ์™„๋ฃŒ');
});

writeStream.write('์ด ๊ธ€์„ ์”๋‹ˆ๋‹ค.\n');
writeStream.write('ํ•œ ๋ฒˆ ๋” ์”๋‹ˆ๋‹ค.');
writeStream.end();

โœ”๊ฒฐ๊ณผ

14. ์ŠคํŠธ๋ฆผ ์‚ฌ์ด์— pipe ์‚ฌ์šฉํ•˜๊ธฐ

pipe๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ŠคํŠธ๋ฆผ์„ ์ด์„ ์ˆ˜ ์žˆ์Œ

  • ์ŠคํŠธ๋ฆผ์œผ๋กœ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ์˜ˆ์ œ

๐Ÿ”ปreadme4.txt

์ €๋ฅผ writeme3.txt๋กœ ๋ณด๋‚ด์ฃผ์„ธ์š”.

๐Ÿ”ปpipe.js

const fs = require('fs');

const readStream = fs.createReadStream('readme4.txt');
const writeStream = fs.createWriteStream('writeme3.txt');
readStream.pipe(writeStream);

โœ”๊ฒฐ๊ณผ

15. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ŠคํŠธ๋ฆผ ์—ฐ๊ฒฐํ•˜๊ธฐ

pipe๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ŠคํŠธ๋ฆผ์„ ์ด์„ ์ˆ˜ ์žˆ์Œ

  • ํŒŒ์ผ์„ ์••์ถ•ํ•œ ํ›„ ๋ณต์‚ฌํ•˜๋Š” ์˜ˆ์ œ
  • ์••์ถ•์—๋Š” zlib ๋‚ด์žฅ ๋ชจ๋“ˆ ์‚ฌ์šฉ(createGzip์œผ๋กœ .gz ํŒŒ์ผ ์ƒ์„ฑ)![]

๐Ÿ”ปgzip.js

const zlib = require('zlib');
const fs = require('fs');

const readStream = fs.createReadStream('./readme4.txt');
const zlibStream = zlib.createGzip();
const writeStream = fs.createWriteStream('./readme4.txt.gz');
readStream.pipe(zlibStream).pipe(writeStream);

โœ”๊ฒฐ๊ณผ

16. ํฐ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

1GB ์ •๋„์˜ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ด„.
createWriteStream์œผ๋กœ ๋งŒ๋“ค์–ด์•ผ ๋ฉ”๋ชจ๋ฆฌ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธฐ์ง€ ์•Š์Œ.

๐Ÿ”ปcreatebigfile.js

const fs = require('fs');
const file = fs.createWriteStream('./big.txt');

for (let i = 0; i <= 10000000; i++) {
  file.write('์•ˆ๋…•ํ•˜์„ธ์š”. ์—„์ฒญ๋‚˜๊ฒŒ ํฐ ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด ๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ์˜ค ๋‹จ๋‹จํžˆ ํ•˜์„ธ์š”!\n');
}
file.end();

17. ๋ฉ”๋ชจ๋ฆฌ ์ฒดํฌํ•˜๊ธฐ

๋ฒ„ํผ ๋ฐฉ์‹๊ณผ ์ŠคํŠธ๋ฆผ ๋ฐฉ์‹ ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์„ ๋น„๊ตํ•ด๋ณด๊ธฐ

๐Ÿ”ปbuffer-memory.js

const fs = require('fs');

console.log('before: ', process.memoryUsage().rss);

const data1 = fs.readFileSync('./big.txt');
fs.writeFileSync('./big2.txt', data1);
console.log('buffer: ', process.memoryUsage().rss);

โœ”๊ฒฐ๊ณผ


๐Ÿ”ปstream-memory.js

const fs = require('fs');

console.log('before: ', process.memoryUsage().rss);

const readStream = fs.createReadStream('./big.txt');
const writeStream = fs.createWriteStream('./big3.txt');
readStream.pipe(writeStream);
readStream.on('end', () => {
  console.log('stream: ', process.memoryUsage().rss);
});

โœ”๊ฒฐ๊ณผ

18. ๊ธฐํƒ€ fs ๋ฉ”์„œ๋“œ

ํŒŒ์ผ ๋ฐ ํด๋” ์ƒ์„ฑ

๐Ÿ”ปfscreate.js

const fs = require('fs');

fs.access('./folder', fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK, (err) => {
  if (err) {
    if (err.code === 'ENOENT') {
      console.log('ํด๋” ์—†์Œ');
      fs.mkdir('./folder', (err) => {
        if (err) {
          throw err;
        }
        console.log('ํด๋” ๋งŒ๋“ค๊ธฐ ์„ฑ๊ณต');
        fs.open('./folder/file.js', 'w', (err, fd) => {
          if (err) {
            throw err;
          }
          console.log('๋นˆ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ ์„ฑ๊ณต', fd);
          fs.rename('./folder/file.js', './folder/newfile.js', (err) => {
            if (err) {
              throw err;
            }
            console.log('์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ ์„ฑ๊ณต');
          });
        });
      });
    } else {
      throw err;
    }
  } else {
    console.log('์ด๋ฏธ ํด๋” ์žˆ์Œ');
  }
});

โœ”๊ฒฐ๊ณผ

19. access, mkdir, open, rename

ํŒŒ์ผ ๋ฐ ํด๋” ์ƒ์„ฑ, ์‚ญ์ œ

  • fs.access(๊ฒฝ๋กœ, ์˜ต์…˜, ์ฝœ๋ฐฑ): ํด๋”๋‚˜ ํŒŒ์ผ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์ƒ์ˆ˜๋“ค์„ ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. F_OK๋Š” ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€, R_OK๋Š” ์ฝ๊ธฐ ๊ถŒํ•œ ์—ฌ๋ถ€, W_OK๋Š” ์“ฐ๊ธฐ ๊ถŒํ•œ ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ/ํด๋”๋‚˜ ๊ถŒํ•œ์ด ์—†๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ํŒŒ์ผ/ํด๋”๊ฐ€ ์—†์„ ๋•Œ์˜ ์—๋Ÿฌ ์ฝ”๋“œ๋Š” ENOENT์ž…๋‹ˆ๋‹ค.
  • fs.mkdir(๊ฒฝ๋กœ, ์ฝœ๋ฐฑ): ํด๋”๋ฅผ ๋งŒ๋“œ๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ด๋ฏธ ํด๋”๊ฐ€ ์žˆ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋จผ์ € access() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
  • fs.open(๊ฒฝ๋กœ, ์˜ต์…˜, ์ฝœ๋ฐฑ): ํŒŒ์ผ์˜ ์•„์ด๋””(fd ๋ณ€์ˆ˜)๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ ๋’ค ๊ทธ ์•„์ด๋””๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ๊ฐ€์ ธ์˜จ ์•„์ด๋””๋ฅผ ์‚ฌ์šฉํ•ด fs.read()๋‚˜ fs.write()๋กœ ์ฝ๊ฑฐ๋‚˜ ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ์–ด๋–ค ๋™์ž‘์„ ํ•  ๊ฒƒ์ธ์ง€ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์“ฐ๋ ค๋ฉด w, ์ฝ์œผ๋ ค๋ฉด r, ๊ธฐ์กด ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด a์ž…๋‹ˆ๋‹ค. ์˜ˆ์ œ์—์„œ๋Š” w๋กœ ์„ค์ •ํ–ˆ์œผ๋ฏ€๋กœ ํŒŒ์ผ์ด ์—†์„ ๋•Œ ์ƒˆ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. r์ด์—ˆ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • fs.rename(๊ธฐ์กด ๊ฒฝ๋กœ, ์ƒˆ ๊ฒฝ๋กœ, ์ฝœ๋ฐฑ): ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ๊ธฐ์กด ํŒŒ์ผ ์œ„์น˜์™€ ์ƒˆ๋กœ์šด ํŒŒ์ผ ์œ„์น˜๋ฅผ ์ ์–ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ๊ฐ™์€ ํด๋”๋ฅผ ์ง€์ •ํ•  ํ•„์š”๋Š” ์—†์œผ๋ฏ€๋กœ ์ž˜๋ผ๋‚ด๊ธฐ ๊ฐ™์€ ๊ธฐ๋Šฅ์„ ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

20. ํด๋” ๋‚ด์šฉ ํ™•์ธ ๋ฐ ์‚ญ์ œ

  • fs.readdir(๊ฒฝ๋กœ, ์ฝœ๋ฐฑ): ํด๋” ์•ˆ์˜ ๋‚ด์šฉ๋ฌผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด ์•ˆ์— ๋‚ด๋ถ€ ํŒŒ์ผ๊ณผ ํด๋”๋ช…์ด ๋‚˜์˜ต๋‹ˆ๋‹ค.
  • fs.unlink(๊ฒฝ๋กœ, ์ฝœ๋ฐฑ): ํŒŒ์ผ์„ ์ง€์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋จผ์ € ํŒŒ์ผ์ด ์žˆ๋Š”์ง€๋ฅผ ๊ผญ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • fs.rmdir(๊ฒฝ๋กœ, ์ฝœ๋ฐฑ): ํด๋”๋ฅผ ์ง€์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํด๋” ์•ˆ์— ํŒŒ์ผ์ด ์žˆ๋‹ค๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ๋จผ์ € ๋‚ด๋ถ€ ํŒŒ์ผ์„ ๋ชจ๋‘ ์ง€์šฐ๊ณ  ํ˜ธ์ถœํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ”ปfsdelete.js

const fs = require('fs');

fs.readdir('./folder', (err, dir) => {
  if (err) {
    throw err;
  }
  console.log('ํด๋” ๋‚ด์šฉ ํ™•์ธ', dir);
  fs.unlink('./folder/newfile.js', (err) => {
    if (err) {
      throw err;
    }
    console.log('ํŒŒ์ผ ์‚ญ์ œ ์„ฑ๊ณต');
    fs.rmdir('./folder', (err) => {
      if (err) {
        throw err;
      }
      console.log('ํด๋” ์‚ญ์ œ ์„ฑ๊ณต');
    });
  });
});

โœ”๊ฒฐ๊ณผ

21. ๊ธฐํƒ€ fs ๋ฉ”์„œ๋“œ

ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

๐Ÿ”ปcopyfile.js

const fs = require('fs');

fs.copyFile('readme4.txt', 'writeme4.txt', (error) => {
  if (error) {
    return console.error(error);
  }
  console.log('๋ณต์‚ฌ ์™„๋ฃŒ');
});

โœ”๊ฒฐ๊ณผ

22. ๊ธฐํƒ€ fs ๋ฉ”์„œ๋“œ

ํŒŒ์ผ์„ ๊ฐ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•(๋ณ€๊ฒฝ ์‚ฌํ•ญ ๋ฐœ์ƒ ์‹œ ์ด๋ฒคํŠธ ํ˜ธ์ถœ)

๐Ÿ”ปwatch.js

const fs = require('fs');

fs.watch('./target.txt', (eventType, filename) => {
  console.log(eventType, filename);
});

โœ”๊ฒฐ๊ณผ

23. ์Šค๋ ˆ๋“œํ’€ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ”ปfs, crypto, zlib ๋ชจ๋“ˆ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ๋Š” ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๋™์‹œ์— ์‹คํ–‰๋จ

  • ์Šค๋ ˆ๋“œํ’€์ด ๋™์‹œ์— ์ฒ˜๋ฆฌํ•ด์คŒ

threadpool.js

const crypto = require('crypto');

const pass = 'pass';
const salt = 'salt';
const start = Date.now();

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('1:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('2:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('3:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('4:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('5:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('6:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('7:', Date.now() - start);
});

crypto.pbkdf2(pass, salt, 1000000, 128, 'sha512', () => {
  console.log('8:', Date.now() - start);
});

โœ”๊ฒฐ๊ณผ

24. UV_THREAD_SIZE

์Šค๋ ˆ๋“œํ’€์„ ์ง์ ‘ ์ปจํŠธ๋กคํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ ๊ฐœ์ˆ˜ ์กฐ์ ˆ์€ ๊ฐ€๋Šฅ

  • ์œˆ๋„์šฐ๋ผ๋ฉด ํ„ฐ๋ฏธ๋„์— SET UV_THREADPOOL_SIZE=๊ฐœ์ˆ˜
  • ๋งฅ, ๋ฆฌ๋ˆ…์Šค๋ผ๋ฉด UV_THREADPOOL_SIZE=๊ฐœ์ˆ˜
  • ์ด์ „ ์˜ˆ์ œ๋ฅผ ์Šค๋ ˆ๋“œํ’€ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ”๊พผ ๋’ค ์žฌ์‹คํ–‰ํ•ด๋ณด๊ธฐ

์ด๋ฒคํŠธ ์ดํ•ดํ•˜๊ธฐ

1. ์ด๋ฒคํŠธ ๋งŒ๋“ค๊ณ  ํ˜ธ์ถœํ•˜๊ธฐ

events ๋ชจ๋“ˆ๋กœ ์ปค์Šคํ…€ ์ด๋ฒคํŠธ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

  • ์ŠคํŠธ๋ฆผ์— ์“ฐ์˜€๋˜ on(โ€˜dataโ€™), on(โ€˜endโ€™) ๋“ฑ๊ณผ ๋น„๊ต
  • on(์ด๋ฒคํŠธ๋ช…, ์ฝœ๋ฐฑ): ์ด๋ฒคํŠธ ์ด๋ฆ„๊ณผ ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ์˜ ์ฝœ๋ฐฑ์„ ์—ฐ๊ฒฐํ•ด์ค๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ์—ฐ๊ฒฐํ•˜๋Š” ๋™์ž‘์„ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋‹์ด๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค. event2์ฒ˜๋Ÿผ ์ด๋ฒคํŠธ ํ•˜๋‚˜์— ์ด๋ฒคํŠธ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋‹ฌ์•„์ค„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
  • addListener(์ด๋ฒคํŠธ๋ช…, ์ฝœ๋ฐฑ): on๊ณผ ๊ธฐ๋Šฅ์ด ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • emit(์ด๋ฒคํŠธ๋ช…): ์ด๋ฒคํŠธ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ ์ด๋ฆ„์„ ์ธ์ž๋กœ ๋„ฃ์–ด์ฃผ๋ฉด ๋ฏธ๋ฆฌ ๋“ฑ๋กํ•ด๋’€๋˜ ์ด๋ฒคํŠธ ์ฝœ๋ฐฑ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • once(์ด๋ฒคํŠธ๋ช…, ์ฝœ๋ฐฑ): ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๋Š” ์ด๋ฒคํŠธ์ž…๋‹ˆ๋‹ค. myEvent.emit('event3')์„ ๋‘ ๋ฒˆ ์—ฐ์† ํ˜ธ์ถœํ–ˆ์ง€๋งŒ ์ฝœ๋ฐฑ์ด ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • removeAllListeners(์ด๋ฒคํŠธ๋ช…): ์ด๋ฒคํŠธ์— ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.event4๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— ๋ฆฌ์Šค๋„ˆ๋ฅผ ์ œ๊ฑฐํ–ˆ์œผ๋ฏ€๋กœ event4์˜ ์ฝœ๋ฐฑ์€ ํ˜ธ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • removeListener(์ด๋ฒคํŠธ๋ช…, ๋ฆฌ์Šค๋„ˆ): ์ด๋ฒคํŠธ์— ์—ฐ๊ฒฐ๋œ ๋ฆฌ์Šค๋„ˆ๋ฅผ ํ•˜๋‚˜์”ฉ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ์—ญ์‹œ* event5์˜ ์ฝœ๋ฐฑ๋„ ํ˜ธ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • off(์ด๋ฒคํŠธ๋ช…, ์ฝœ๋ฐฑ): ๋…ธ๋“œ 10 ๋ฒ„์ „์—์„œ ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ๋กœ, removeListener์™€ ๊ธฐ๋Šฅ์ด ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • listenerCount(์ด๋ฒคํŠธ๋ช…): ํ˜„์žฌ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ๋ช‡ ๊ฐœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

2. ์ปค์Šคํ…€ ์ด๋ฒคํŠธ ์˜ˆ์ œ

๐Ÿ”ปevent.js

const EventEmitter = require('events');

const myEvent = new EventEmitter();
myEvent.addListener('event1', () => {
  console.log('์ด๋ฒคํŠธ 1');
});
myEvent.on('event2', () => {
  console.log('์ด๋ฒคํŠธ 2');
});
myEvent.on('event2', () => {
  console.log('์ด๋ฒคํŠธ 2 ์ถ”๊ฐ€');
});
myEvent.once('event3', () => {
  console.log('์ด๋ฒคํŠธ 3');
}); // ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋จ

myEvent.emit('event1'); // ์ด๋ฒคํŠธ ํ˜ธ์ถœ
myEvent.emit('event2'); // ์ด๋ฒคํŠธ ํ˜ธ์ถœ

myEvent.emit('event3');
myEvent.emit('event3'); // ์‹คํ–‰ ์•ˆ ๋จ

myEvent.on('event4', () => {
  console.log('์ด๋ฒคํŠธ 4');
});
myEvent.removeAllListeners('event4');
myEvent.emit('event4'); // ์‹คํ–‰ ์•ˆ ๋จ

const listener = () => {
  console.log('์ด๋ฒคํŠธ 5');
};
myEvent.on('event5', listener);
myEvent.removeListener('event5', listener);
myEvent.emit('event5'); // ์‹คํ–‰ ์•ˆ ๋จ

console.log(myEvent.listenerCount('event2'));

โœ”๊ฒฐ๊ณผ

์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•˜๊ธฐ

1. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

์˜ˆ์™ธ(Exception): ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•œ ์—๋Ÿฌ

  • ๋…ธ๋“œ ํ”„๋กœ์„ธ์Šค/์Šค๋ ˆ๋“œ๋ฅผ ๋ฉˆ์ถค
  • ๋…ธ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ฑ๊ธ€ ์Šค๋ ˆ๋“œ๋ผ ์Šค๋ ˆ๋“œ๊ฐ€ ๋ฉˆ์ถ˜๋‹ค๋Š” ๊ฒƒ์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ ๋ฉˆ์ถ”๋Š” ๊ฒƒ
  • ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋Š” ํ•„์ˆ˜

2. try catch๋ฌธ

๊ธฐ๋ณธ์ ์œผ๋กœ try catch๋ฌธ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌ

  • ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๋งŒํ•œ ๊ณณ์„ try catch๋กœ ๊ฐ์Œˆ

๐Ÿ”ปerror1.js

setInterval(() => {
  console.log('์‹œ์ž‘');
  try {
    throw new Error('์„œ๋ฒ„๋ฅผ ๊ณ ์žฅ๋‚ด์ฃผ๋งˆ!');
  } catch (err) {
    console.error(err);
  }
}, 1000);

โœ”๊ฒฐ๊ณผ

3. ๋…ธ๋“œ ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ์˜ ์—๋Ÿฌ

๋…ธ๋“œ ๋น„๋™๊ธฐ ๋ฉ”์„œ๋“œ์˜ ์—๋Ÿฌ๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋จ

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ ์—๋Ÿฌ ๊ฐ์ฒด ์ œ๊ณต

๐Ÿ”ปerror2.js

const fs = require('fs');

setInterval(() => {
  fs.unlink('./abcdefg.js', (err) => {
    if (err) {
      console.error(err);
    }
  });
}, 1000);

โœ”๊ฒฐ๊ณผ

4. ํ”„๋กœ๋ฏธ์Šค์˜ ์—๋Ÿฌ

ํ”„๋กœ๋ฏธ์Šค์˜ ์—๋Ÿฌ๋Š” ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์•„๋„ ๋จ

  • ๋ฒ„์ „์ด ์˜ฌ๋ผ๊ฐ€๋ฉด ๋™์ž‘์ด ๋ฐ”๋€” ์ˆ˜ ์žˆ์Œ

๐Ÿ”ปerror3.js

const fs = require('fs').promises;

setInterval(() => {
  fs.unlink('./abcdefg.js')
}, 1000);

โœ”๊ฒฐ๊ณผ

5. ์˜ˆ์ธก ๋ถˆ๊ฐ€๋Šฅํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌํ•˜๊ธฐ

์ตœํ›„์˜ ์ˆ˜๋‹จ์œผ๋กœ ์‚ฌ์šฉ

  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ ๋™์ž‘์ด ๋ณด์žฅ๋˜์ง€ ์•Š์Œ
  • ๋”ฐ๋ผ์„œ ๋ณต๊ตฌ ์ž‘์—…์šฉ์œผ๋กœ ์“ฐ๋Š” ๊ฒƒ์€ ๋ถ€์ ํ•ฉ
  • ์—๋Ÿฌ ๋‚ด์šฉ ๊ธฐ๋ก ์šฉ์œผ๋กœ๋งŒ ์“ฐ๋Š” ๊ฒŒ ์ข‹์Œ

๐Ÿ”ปerror4.js

process.on('uncaughtException', (err) => {
  console.error('์˜ˆ๊ธฐ์น˜ ๋ชปํ•œ ์—๋Ÿฌ', err);
});

setInterval(() => {
  throw new Error('์„œ๋ฒ„๋ฅผ ๊ณ ์žฅ๋‚ด์ฃผ๋งˆ!');
}, 1000);

setTimeout(() => {
  console.log('์‹คํ–‰๋ฉ๋‹ˆ๋‹ค');
}, 2000);

โœ”๊ฒฐ๊ณผ

6. ํ”„๋กœ์„ธ์Šค ์ข…๋ฃŒํ•˜๊ธฐ

์œˆ๋„

๋งฅ/๋ฆฌ๋ˆ…์Šค

๐Ÿ˜ƒ์ถœ์ฒ˜๐Ÿ˜ƒ
Node.js ๊ต๊ณผ์„œ - ๊ธฐ๋ณธ๋ถ€ํ„ฐ ํ”„๋กœ์ ํŠธ ์‹ค์Šต๊นŒ์ง€
https://www.inflearn.com/course/%EB%85%B8%EB%93%9C-%EA%B5%90%EA%B3%BC%EC%84%9C/dashboard

0๊ฐœ์˜ ๋Œ“๊ธ€