๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
Development/Node.js

[Node.js] Node.js Modules | childProcess, spawn, stdout, stderr, fs, path, yargs, properties-reader, Buffer

by DevIseo 2023. 6. 16.

Node.js modules

๐Ÿ—จ๏ธ ChildProcess

 โœ… ChildProcess

๊ฐœ๋…

  • Node.js ํ”„๋กœ์„ธ์Šค ๋‚ด์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ์™€ ์ƒํ˜ธ ์ž‘์šฉํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ
  • Node.js์—์„œ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ œ๊ณต
  • exec(), spawn(),fork() ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณต
    • exec()
      • ํ•จ์ˆ˜๋Š” ์…ธ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ•ด๋‹น ๊ฒฐ๊ณผ๋ฅผ ๋ฒ„ํผ๋ง
    • spawn()
      • ์ƒˆ๋กœ์šด ํ”„๋กœ์„ธ์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ํ”„๋กœ์„ธ์Šค์™€์˜ ํ‘œ์ค€ ์ž…์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ์ œ๊ณต
    • fork()
      • spawn() ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ, ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค์™€ ์ž์‹ ํ”„๋กœ์„ธ์Šค ๊ฐ„์— ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” IPC ์ฑ„๋„์„ ์ž๋™์œผ๋กœ ์„ค์ •
  • ChildProcess ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด Node.js ํ”„๋กœ์„ธ์Šค ๋‚ด์—์„œ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ์Œ
    • Node.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์™ธ๋ถ€ ๋ช…๋ น์–ด ์‹คํ–‰, ์ž์‹ ํ”„๋กœ์„ธ์Šค ์ƒ์„ฑ, ํด๋Ÿฌ์Šคํ„ฐ๋ง ๋“ฑ ๋‹ค์–‘ํ•œ ์šฉ๋„๋กœ ํ™œ์šฉ ๊ฐ€๋Šฅ

์‚ฌ์šฉ์˜ˆ์‹œ

Electorn์—์„œ ChildProcess๋ฅผ ์‚ฌ์šฉํ•ด ์™ธ๋ถ€ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์˜ˆ์‹œ

const { exec } = require('child_process');

// 'ls' ๋ช…๋ น์–ด ์‹คํ–‰
exec('ls', (err, stdout, stderr) => {
  if (err) {
    console.error(`exec error: ${err}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
});

 

ChildProcess๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ ๋ฉ”์ธ ํ”„๋กœ์„ธ์Šค์™€ ๋ Œ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค ๊ฐ„์˜ ํ†ต์‹  ์˜ˆ์‹œ

const { fork } = require('child_process');

// ๋žœ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ์‹คํ–‰ํ•  ๋ชจ๋“ˆ ๊ฒฝ๋กœ
const pathToScript = path.join(__dirname, 'myScript.js');

// ๋žœ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ myScript.js ๋ชจ๋“ˆ ์‹คํ–‰
const child = fork(pathToScript);

// ๋žœ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค๋กœ ๋ฉ”์‹œ์ง€ ๋ณด๋‚ด๊ธฐ
child.send('hello from main process!');

// ๋žœ๋”๋Ÿฌ ํ”„๋กœ์„ธ์Šค์—์„œ ๋ฉ”์‹œ์ง€ ๋ฐ›๊ธฐ
child.on('message', (msg) => {
  console.log(`received message from renderer process: ${msg}`);
});

 

child_process ๋ชจ๋“ˆ์˜ spawn() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Windows ์šด์˜์ฒด์ œ์—์„œ cmd.exe๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•˜๋Š” ์˜ˆ์‹œ

const { spawn } = require('child_process');
/*
* ์ƒˆ๋กœ์šด cmd.exe ์‰˜์„ ์—ด๊ณ  'dir' ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅ
*/

// Windows ์šด์˜์ฒด์ œ์—์„œ๋Š” 'cmd.exe'๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ช…๋ น์–ด ์‹คํ–‰
const cmd = spawn('cmd.exe'); 

cmd.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

cmd.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

cmd.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

// ๋ช…๋ น์–ด ์ž…๋ ฅ์„ ์œ„ํ•ด stdin์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†ก
cmd.stdin.write('dir\\n'); // ๋ช…๋ น์–ด ์ „์†ก
cmd.stdin.end(); // ์ž…๋ ฅ ์ข…๋ฃŒ

 

๐Ÿ—จ๏ธ child_process.spawn(command[, args][, options])

 โœ… child_process.spawn(command[, args][, options])

๊ฐœ๋…

  • command
    • ์‹คํ–‰ํ•  ๋ช…๋ น
  • args
    • ๋ฌธ์ž์—ด ์ธ์ˆ˜ ๋ชฉ๋ก
  • options
    • cwd ์ž์‹ ํ”„๋กœ์„ธ์Šค์˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ
    • shell true์ผ ๋•Œ ์‰˜ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰. 

 

๐Ÿ—จ๏ธ ChildProcessWithoutNullStreams

โœ… ChildProcessWithoutNullStreams

๊ฐœ๋…

  • Node.js์˜ child_process ๋ชจ๋“ˆ์—์„œ ์ œ๊ณตํ•˜๋Š” ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜
  • ChildProcess ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ, ํ‘œ์ค€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ๊ณผ ํ‘œ์ค€ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์ด null์ด ์•„๋‹ˆ๋ผ๋ฉด ChildProcessWithoutNullStreams ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
    • ๊ธฐ๋ณธ์ ์œผ๋กœ ChildProcess ํด๋ž˜์Šค์—์„œ๋Š” ํ‘œ์ค€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ๊ณผ ํ‘œ์ค€ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์ด ๋ชจ๋‘ null๋กœ ์„ค์ •
    • ChildProcessWithoutNullStreams์˜ ๊ฒฝ์šฐ ์ด๋Ÿฌํ•œ ์ œํ•œ์„ ์—†์• ๊ณ , ํ‘œ์ค€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ๊ณผ ํ‘œ์ค€ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ํ†ตํ•ด ํ”„๋กœ์„ธ์Šค์™€ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • ChildProcess ํด๋ž˜์Šค์˜ ๊ธฐ๋Šฅ์„ ํ™•์žฅํ•˜์—ฌ ํ”„๋กœ์„ธ์Šค์™€ ๋”์šฑ ํšจ์œจ์ ์œผ๋กœ ์ƒํ˜ธ์ž‘์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค

์‚ฌ์šฉ์˜ˆ์‹œ

const { spawn } = require('child_process');

//stdio: 'inherit' ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ, ๋ถ€๋ชจ ํ”„๋กœ์„ธ์Šค์˜ ํ‘œ์ค€ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ, 
//ํ‘œ์ค€ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ ๋ฐ ํ‘œ์ค€ ์˜ค๋ฅ˜ ์ŠคํŠธ๋ฆผ์„ ์ž์‹ ํ”„๋กœ์„ธ์Šค๋กœ ์ „๋‹ฌ
//ChildProcessWithoutNullStreams ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜

const ls = spawn('ls', ['-lh', '/usr'], { stdio: 'inherit' });

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

 

 

 

๐Ÿ—จ๏ธ stdout, stderr

 โœ… stdout, stderr

๊ฐœ๋…

stdout

  • "standard output"์˜ ์•ฝ์ž
  • ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋  ๋•Œ ์ถœ๋ ฅํ•˜๋Š” ๊ฒฐ๊ณผ๋ฌผ์ด๋‚˜ ๋ฉ”์‹œ์ง€
  • ์ฝ˜์†”(Console)์ด๋‚˜ ํ„ฐ๋ฏธ๋„(Terminal)์— ์ถœ๋ ฅ
  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” stdout์„ ํ‘œ์ค€ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ(Standard output stream)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ„

stderr

  • "standard error"์˜ ์•ฝ์ž
  • ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰ ์ค‘ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅ
  • ์ฝ˜์†”์ด๋‚˜ ํ„ฐ๋ฏธ๋„์—์„œ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์ถœ๋ ฅ
  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ๋Š” stderr์„ ํ‘œ์ค€ ์˜ค๋ฅ˜ ์ŠคํŠธ๋ฆผ(Standard error stream)์ด๋ผ๊ณ ๋„ ๋ถ€๋ฆ„

์‚ฌ์šฉ ์˜ˆ์‹œ

์ž์‹ ํ”„๋กœ์„ธ์Šค์˜ stdout๊ณผ stderr์„ ๊ฐ๊ฐ data ์ด๋ฒคํŠธ์™€ error ์ด๋ฒคํŠธ๋กœ ์บก์ฒ˜

/*
* Node.js์˜ child_process ๋ชจ๋“ˆ์—์„œ๋Š” spawn() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ
*์ž์‹ ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•  ๋•Œ, 
* ์ด๋Ÿฌํ•œ stdout๊ณผ stderr์„ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ํ†ตํ•ด ์บก์ฒ˜
*/

const { spawn } = require('child_process');

const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.error(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});

 

 

 

๐Ÿ—จ๏ธ fs

 โœ… fs

๊ฐœ๋…

  • Node.js์—์„œ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๋ชจ๋“ˆ
  • ํŒŒ์ผ ์ƒ์„ฑ, ์ฝ๊ธฐ, ์“ฐ๊ธฐ, ์ˆ˜์ •, ์‚ญ์ œ ๋“ฑ์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณต
  • Node.js์˜ ๊ธฐ๋ณธ ๋ชจ๋“ˆ๋กœ ์ œ๊ณต๋˜๋ฉฐ, ๋ณ„๋„์˜ ์„ค์น˜ ์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • fs ๋ชจ๋“ˆ์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋“ค์€ ๋Œ€๋ถ€๋ถ„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘, ๋™๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋„ ์ œ๊ณต
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜

์ฃผ์š” ํ•จ์ˆ˜

* `fs.readFile()`: ํŒŒ์ผ์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ์–ด๋“ค์ž…๋‹ˆ๋‹ค.
* `fs.readFileSync()`: ํŒŒ์ผ์„ ๋™๊ธฐ์ ์œผ๋กœ ์ฝ์–ด๋“ค์ž…๋‹ˆ๋‹ค.
* `fs.writeFile()`: ํŒŒ์ผ์„ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์”๋‹ˆ๋‹ค.
* `fs.writeFileSync()`: ํŒŒ์ผ์„ ๋™๊ธฐ์ ์œผ๋กœ ์”๋‹ˆ๋‹ค.
* `fs.appendFile()`: ํŒŒ์ผ์˜ ๋์— ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
* `fs.unlink()`: ํŒŒ์ผ์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ ์˜ˆ์‹œ

const fs = require('fs');
const path = require('path');
const { dialog } = require('electron').remote;

// ํŒŒ์ผ ๋‹ค์ด์–ผ๋กœ๊ทธ๋ฅผ ์—ด์–ด์„œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.
dialog.showOpenDialog({
  properties: ['openFile']
}).then(result => {
  if (!result.canceled) {
    // ์„ ํƒํ•œ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
    const filePath = result.filePaths[0];

    // ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์ž…๋‹ˆ๋‹ค.
    fs.readFile(filePath, 'utf-8', (err, data) => {
      if (err) {
        console.error(err);
        return;
      }
      
      // ํŒŒ์ผ ๋‚ด์šฉ์—์„œ ๋‹จ์–ด ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.
      const wordCount = data.split(/\\s+/).length;

      // ํ™”๋ฉด์— ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
      document.getElementById('result').textContent = `๋‹จ์–ด ์ˆ˜: ${wordCount}`;
    });
  }
}).catch(err => {
  console.error(err);
});

 

 

 

๐Ÿ—จ๏ธ path

 โœ… path

๊ฐœ๋…

  • ํŒŒ์ผ ๊ฒฝ๋กœ์™€ ๊ด€๋ จ๋œ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์ œ๊ณต
  • ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์กฐ์ž‘ํ•˜๊ณ , ํŒŒ์ผ ๊ฒฝ๋กœ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์ถ”์ถœํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ

์‚ฌ์šฉ ์˜ˆ์‹œ

path ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ /path/to/some/file.txt ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ฒ˜๋ฆฌ

const path = require('path');

const fullPath = '/path/to/some/file.txt';

// ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ํŒŒ์ผ ์ด๋ฆ„์„ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
const filename = path.basename(fullPath);
console.log('Filename:', filename);

// ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฝ๋กœ๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
const directory = path.dirname(fullPath);
console.log('Directory:', directory);

// ํŒŒ์ผ ๊ฒฝ๋กœ์—์„œ ํ™•์žฅ์ž๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
const extname = path.extname(fullPath);
console.log('Extension:', extname);

// ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์กฐํ•ฉํ•ฉ๋‹ˆ๋‹ค.
const combinedPath = path.join(directory, 'newfile.md');
console.log('Combined Path:', combinedPath);

// ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
const absolutePath = path.resolve(combinedPath);
console.log('Absolute Path:', absolutePath);

/*
๊ฒฐ๊ณผ๊ฐ’
Filename: file.txt
Directory: /path/to/some
Extension: .txt
Combined Path: \\path\\to\\some\\newfile.md
Absolute Path: C:\\path\\to\\some\\newfile.md
*/

 

 

 

๐Ÿ—จ๏ธ yargs

โœ… yargs

์„ค์น˜

npm i yargs

๊ฐœ๋…

  • ๋ช…๋ นํ–‰ ์ธ์ž(command line arguments)๋ฅผ ํŒŒ์‹ฑ(parsing)ํ•˜๊ธฐ ์œ„ํ•œ ์œ ํ‹ธ๋ฆฌํ‹ฐ
  • ๋ช…๋ นํ–‰ ์ธ์ž๋ฅผ ํŒŒ์‹ฑํ•˜์—ฌ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, ์ธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์คŒ
  • ๋ช…๋ นํ–‰ ์ธ์ž๋ฅผ ์‚ฌ์šฉ์ž ์นœํ™”์ ์ธ ํ˜•ํƒœ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ๋Šฅ๋„ ์ œ๊ณต

์‚ฌ์šฉ ์˜ˆ์‹œ

const yargs = require('yargs');

// ์‚ฌ์šฉ์ž ์นœํ™”์ ์ธ ํ˜•ํƒœ๋กœ ๋ช…๋ นํ–‰ ์ธ์ž๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
const argv = yargs
  .usage('Usage: $0 [options] <path>')
  .option('r', {
    alias: 'recursive',
    describe: 'Recursively process all files in the directory',
    type: 'boolean',
    default: false
  })
  .option('e', {
    alias: 'extension',
    describe: 'Specify the file extension to process',
    type: 'string',
    default: '.txt'
  })
  .help('h')
  .alias('h', 'help')
  .argv;

// ๋ช…๋ นํ–‰ ์ธ์ž๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
console.log('Options:', argv);

// path ์ธ์ž๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
const path = argv._[0];
console.log('Path:', path);

 

 

๐Ÿ—จ๏ธ yargs ์˜ argv

 โœ… argv

๊ฐœ๋…

  • argv๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ๋ช…๋ นํ–‰ ์ธ์ž(command line argument)๋ฅผ ์ „๋‹ฌํ•  ๋•Œ, ์ด๋ฅผ ํŒŒ์‹ฑ(parsing)ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฐ์ฒด
  • argv ๊ฐ์ฒด๋Š” ๋‹ค์–‘ํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์œผ๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ์ „๋‹ฌํ•œ ๋ช…๋ นํ–‰ ์ธ์ž์™€ ์ด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์Œ

์‚ฌ์šฉ ์˜ˆ์‹œ

{
  name: 'Iseo',
  age: 29,
  _: [],
  '$0': 'memo.js'
}
const yargs = require('yargs');

const argv = yargs
  .option('name', {
    alias: 'n',
    describe: 'User name',
    type: 'string'
  })
  .option('age', {
    alias: 'a',
    describe: 'User age',
    type: 'number'
  })
  .argv;

console.log(argv);
{
  name: 'Iseo',
  n: 'Iseo',
  age: 29,
  a: 29,
  _: [],
  '$0': 'memo.js'
}

 

 

 

๐Ÿ—จ๏ธ properties-reader

โœ… properties-reader

๊ฐœ๋…

  • Node.js์—์„œ ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ(.properties)์„ ์ฝ์–ด์™€์„œ JavaScript ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋„๊ตฌ
  • ๋ณ„๋„์˜ JSON ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ ๋„, ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ์„ค์ • ์ •๋ณด๋ฅผ ๊ด€๋ฆฌ
  • Electron ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์„ค์ • ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐ์— ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ

์‚ฌ์šฉ ์˜ˆ์‹œ

# config.properties

host=localhost
port=8080
database=mydb
const PropertiesReader = require('properties-reader');

// config.properties ํŒŒ์ผ์„ ์ฝ์–ด์™€ PropertiesReader๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
//.path() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ  ๊ฐ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผ ๊ฐ€๋Šฅ
const properties = PropertiesReader('config.properties').path();

console.log(properties.host); // localhost
console.log(properties.port); // 8080
console.log(properties.database); // mydb

 

 

 

๐Ÿ—จ๏ธ fs.existsSync()

 โœ… fs.existSync()

๊ฐœ๋…

  • ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
  • ๋™๊ธฐ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋ฉฐ, ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋ฉด **true**๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด **false**๋ฅผ ๋ฐ˜ํ™˜

์‚ฌ์šฉ ์˜ˆ์‹œ

const fs = require('fs');

if (fs.existsSync('/path/to/file')) {
  console.log('File exists');
} else {
  console.log('File does not exist');
}

 

 

๐Ÿ—จ๏ธBuffer

 โœ… Buffer

๊ฐœ๋…

  • Node.js์˜ **Buffer**ํด๋ž˜์Šค๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ผ์ข…์˜ ๋ฐฐ์—ด
  • ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—์„œ ์ผ์ •ํ•œ ํฌ๊ธฐ์˜ ๊ณต๊ฐ„์„ ํ™•๋ณดํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜๊ณ  ๋‹ค๋ฅธ ์‹œ์Šคํ…œ๊ณผ ์ƒํ˜ธ์ž‘์šฉ
  • **Buffer**๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๋Š” ๋ฐ ์œ ์šฉํ•œ ๋ฉ”์†Œ๋“œ์™€ ์†์„ฑ์„ ์ œ๊ณต
  • **Buffer**ํด๋ž˜์Šค๋Š” Node.js์˜ I/O ์ž‘์—…์—์„œ ๋งŽ์ด ์‚ฌ์šฉ

์‚ฌ์šฉ ์˜ˆ์‹œ

  • ๋ฌธ์ž์—ด์„ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋กœ ๋ณ€ํ™˜
const str = 'Hello, world!';
const buf = Buffer.from(str, 'utf8');

console.log(buf); // <Buffer 48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21>
  • ๊ณ ์ • ํฌ๊ธฐ์˜ **Buffer**์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์‹œ
const buf1 = Buffer.alloc(10);
const buf2 = Buffer.allocUnsafe(10);

console.log(buf1); // <Buffer 00 00 00 00 00 00 00 00 00 00>
console.log(buf2); // <Buffer 58 0f 70 08 30 00 00 00 00 00>
  • ํŒŒ์ผ์„ ์ฝ๊ณ  ์“ฐ๋Š” ์˜ˆ์‹œ
const fs = require('fs');

// ํŒŒ์ผ์„ ์ฝ์–ด๋“ค์ž…๋‹ˆ๋‹ค.
fs.readFile('input.txt', (err, data) => {
  if (err) throw err;

  // ์ฝ์–ด๋“ค์ธ ๋ฐ์ดํ„ฐ๋ฅผ `Buffer` ์ธ์Šคํ„ด์Šค๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  const buf = Buffer.from(data);

  // `Buffer` ์ธ์Šคํ„ด์Šค๋ฅผ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
  console.log(buf);
  
  // ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์“ฐ๊ธฐ ์œ„ํ•œ `Buffer` ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  const writeBuf = Buffer.from('Write this data to file', 'utf8');
  
  // ํŒŒ์ผ์— ๋ฐ์ดํ„ฐ๋ฅผ ์”๋‹ˆ๋‹ค.
  fs.writeFile('output.txt', writeBuf, (err) => {
    if (err) throw err;
    
    console.log('Data written to file');
  });
});

 

๋Œ“๊ธ€