๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daily Front_Minhhk

[JS] ํƒ€์ด๋จธAPI[setTimeout,setInterval() ] / (node.js)-fs.readFile(path[, options], callback) ๋ณธ๋ฌธ

Code๊ฐœ๋ฐœ์ผ์ง€

[JS] ํƒ€์ด๋จธAPI[setTimeout,setInterval() ] / (node.js)-fs.readFile(path[, options], callback)

Minhhk 2022. 11. 24. 00:57
๐Ÿ“Œ ์Šฌ์Šฌ ๋น„๋™๊ธฐ ์‹œ๊ฐ„ ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ๋จธ๋ฆฌ๊ฐ€ ๋ณต์žก๊ณผ ์–ด๋ ต๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค.. ์•ž์œผ๋กœ ์„œ๋ฒ„ ์ฃผ๊ณ  ๋ฐ›๊ณ  ๋„คํŠธ์›Œํฌ ํ˜•์„ฑ์ด๋ผ๊ณ  ํ•ด์•ผํ•˜๋‚˜? ํ• ๋ ค๋ฉด ๋น„๋™๊ธฐ ๊ณ„์† ์“ธํ…๋ฐ… ๊ทน๋ณตํ•ด์•ผํ•œ๋‹ค ํ•˜๊ฒ ์ง€?

 

ํƒ€์ด๋จธ ๊ด€๋ จ API

setTimeout(), clearTimeout() // setInterval(), clearInterval(id)

setTimeout(callback, millisecond)

์ผ์ • ์‹œ๊ฐ„ ํ›„์— ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰

  • ๋งค๊ฐœ๋ณ€์ˆ˜(parameter): ์‹คํ–‰ํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜, ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์‹คํ–‰ ์ „ ๊ธฐ๋‹ค๋ ค์•ผ ํ•  ์‹œ๊ฐ„ (๋ฐ€๋ฆฌ์ดˆ)
  • return ๊ฐ’: ์ž„์˜์˜ ํƒ€์ด๋จธ ID
setTimeout(function () {
  console.log('1์ดˆ ํ›„ ์‹คํ–‰');
}, 1000);
// 123

clearTimeout(timerId)

setTimeout ํƒ€์ด๋จธ๋ฅผ ์ข…๋ฃŒ

  • ๋งค๊ฐœ๋ณ€์ˆ˜(parameter): ํƒ€์ด๋จธ ID
  • return ๊ฐ’: ์—†์Œ
const timer = setTimeout(function () {
  console.log('10์ดˆ ํ›„ ์‹คํ–‰');
}, 10000);
clearTimeout(timer);
// setTimeout์ด ์ข…๋ฃŒ๋จ.

setInterval(callback, millisecond)

์ผ์ • ์‹œ๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ๊ฐ€์ง€๊ณ  ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹คํ–‰

  • ๋งค๊ฐœ๋ณ€์ˆ˜(parameter): ์‹คํ–‰ํ•  ์ฝœ๋ฐฑ ํ•จ์ˆ˜, ๋ฐ˜๋ณต์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•œ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ (๋ฐ€๋ฆฌ์ดˆ)
  • return ๊ฐ’: ์ž„์˜์˜ ํƒ€์ด๋จธ ID
setInterval(function () {
  console.log('1์ดˆ๋งˆ๋‹ค ์‹คํ–‰');
}, 1000);
// 345

clearInterval(timerId)

setInterval ํƒ€์ด๋จธ๋ฅผ ์ข…๋ฃŒ

  • ๋งค๊ฐœ๋ณ€์ˆ˜: ํƒ€์ด๋จธ ID
  • return ๊ฐ’: ์—†์Œ
const timer = setInterval(function () {
  console.log('1์ดˆ๋งˆ๋‹ค ์‹คํ–‰');
}, 1000);
clearInterval(timer);
// setInterval์ด ์ข…๋ฃŒ๋จ.

ํ•™์Šต ๋ชฉํ‘œ

  • callback, Promise, async/await ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•œ๋‹ค.
  • Promise ์‹คํ–‰ ํ•จ์ˆ˜๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ resolve ์™€ reject๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • new Promise()๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ Promise ์ธ์Šคํ„ด์Šค๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์„œ๋“œ์˜ ์šฉ๋„๋ฅผ ์ดํ•ดํ•œ๋‹ค.
  • Promise์˜ ์„ธ ๊ฐ€์ง€ ์ƒํƒœ๋Š” ๊ฐ๊ฐ ๋ฌด์—‡์ธ์ง€ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • async/await ํ‚ค์›Œ๋“œ์™€ ํ•จ๊ป˜ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜๋Š” ์–ด๋–ค ํƒ€์ž…์ด์–ด์•ผ ํ•˜๋Š”์ง€ ์ดํ•ดํ•œ๋‹ค.
  • await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์–ด๋–ค ๊ฐ’์ด ๋ฆฌํ„ด๋˜๋Š”์ง€ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

ํƒ€์ด๋จธAPI -> part

01_callback.js

const delay = (wait, callback) => {
  setTimeout(callback, wait);
}

02_promiseConstructor.js

const sleep = (wait) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve('hello');
    }, wait);
  });
}

// catch ์“ธ๋•?

const sleep = (wait) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      reject(new Error('์—๋Ÿฌ'));
    }, wait);
  });
};

script.js

// ์ฝœ๋ฐฑ
function runCallback() {
  resetTitle();
  playVideo();

  delay(1000, () => {
    pauseVideo();
    displayTitle();

    delay(500, () => {
      highlightTitle();

      delay(2000, resetTitle);
    });
  });
}

// Promise
function runPromise() {
  resetTitle();
  playVideo();

  sleep(1000)
    .then((param) => {
      console.log(param);
      pauseVideo();
      displayTitle();
      return "world";
    })
    .then((param) => {
      console.log(param);
      return sleep(5000);
    })
    .then(highlightTitle)
    .then(sleep.bind(null, 2000))
    .then(resetTitle)
    .catch(err => {           //catch +
      console.log(err);
    })
}

// async await
async function runAsync() {
  resetTitle();
  playVideo();

  await sleep(1000);
  pauseVideo();
  displayTitle();

  await sleep(500);
  highlightTitle();

  await sleep(2000);
  resetTitle();
}

 

 

  • Promise ์‹คํ–‰ ํ•จ์ˆ˜๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋‘ ๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ resolve ์™€ reject ๋Š” ๊ฐ๊ฐ ๋ฌด์—‡์„ ์˜๋ฏธํ•˜๋‚˜์š”?
    • executor๋Š” resolve ๋ฐ reject ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ์‹คํ–‰ํ•จ์ˆ˜.
    • ๋น„๋™๊ธฐ ์ž‘์—…์„ ์‹คํ–‰ํ•œ ํ›„ ๋ชจ๋“  ์ž‘์—…์„ ๋๋‚ด๋ฉด resolve๋ฅผ ํ˜ธ์ถœํ•ด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ดํ–‰, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒฝ์šฐ reject๋ฅผ ํ˜ธ์ถœํ•ด ๊ฑฐ๋ถ€

 

  • resolve, rejectํ•จ์ˆ˜์—๋Š” ์ „๋‹ฌ์ธ์ž๋ฅผ ๋„˜๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ ๋„˜๊ธฐ๋Š” ์ „๋‹ฌ์ธ์ž๋Š” ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‚˜์š”?
    • ํ”„๋กœ๋ฏธ์Šค์—์„œ resolve(arg)๊ฐ€ ์‹คํ–‰๋  ๊ฒฝ์šฐ.then(arg)๋ฅผ ์ด์šฉํ•ด ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    • ํ”„๋กœ๋ฏธ์Šค์—์„œ reject(arg)๊ฐ€ ์‹คํ–‰๋  ๊ฒฝ์šฐ.then().catch(arg)๋ฅผ ์ด์šฉํ•ด ์ธ์ž๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

 

  • new Promise()๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ Promise ์ธ์Šคํ„ด์Šค์—๋Š” ์–ด๋–ค ๋ฉ”์„œ๋“œ๊ฐ€ ์กด์žฌํ•˜๋‚˜์š”? ๊ฐ๊ฐ์€ ์–ด๋–ค ์šฉ๋„์ธ๊ฐ€์š”?
    • catch()
      • ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฑฐ๋ถ€๋œ๋‹ค๋ฉด catch()๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  ์ฝœ๋ฐฑ์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    • then()
      • ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ์ดํ–‰๋œ๋‹ค๋ฉด then์„ ์ด์šฉํ•ด ๋ฐ˜ํ™˜๊ฐ’์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ์ƒˆ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค..
    • finally()
      • ํ”„๋กœ๋ฏธ์Šค ์ดํ–‰ ์—ฌ๋ถ€์— ๊ด€๊ณ„์—†์ด ๋ฌด์กฐ๊ฑด ํ˜ธ์ถœ๋œ๋‹ค.
      • reseolve → ์„ฑ๊ณต ๊ฐ’, reject → ์‹คํŒจ ๊ฐ’

 

  • Promise.prototype.then ๋ฉ”์„œ๋“œ๋Š” ๋ฌด์—‡์„ ๋ฆฌํ„ดํ•˜๋‚˜์š”? → resolve์˜ ์ „๋‹ฌ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ ๊ฐ’
...then((data, error) => { })
  • Promise.prototype.catch ๋ฉ”์„œ๋“œ๋Š” ๋ฌด์—‡์„ ๋ฆฌํ„ดํ•˜๋‚˜์š”? → reject์˜ ์ „๋‹ฌ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ ๊ฐ’

 

  • Promise์˜ ์„ธ ๊ฐ€์ง€ ์ƒํƒœ๋Š” ๊ฐ๊ฐ ๋ฌด์—‡์ด๋ฉฐ, ์–ด๋–ค ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋‚˜์š”?
    • Pending(๋Œ€๊ธฐ) : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ๋กœ์ง์ด ์•„์ง ์™„๋ฃŒ๋˜์ง€ ์•Š์€ ์ƒํƒœ
    • Fulfilled(์ดํ–‰) : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์™„๋ฃŒ๋˜์–ด ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ด์ค€ ์ƒํƒœ
    • Rejected(์‹คํŒจ) : ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๊ฐ€ ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ƒํƒœ

 

  • await ํ‚ค์›Œ๋“œ ๋‹ค์Œ์— ๋“ฑ์žฅํ•˜๋Š” ํ•จ์ˆ˜ ์‹คํ–‰์€ ์–ด๋–ค ํƒ€์ž…์„ ๋ฆฌํ„ดํ•  ๊ฒฝ์šฐ์—๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‚˜์š”?
    • Promise ํƒ€์ž…์„ ๋ฆฌํ„ดํ•œ ๊ฒฝ์šฐ์—๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค.
  • await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, ์–ด๋–ค ๊ฐ’์ด ๋ฆฌํ„ด๋˜๋‚˜์š”?
    • Promise ์—์„œ fulfill๋œ ๊ฐ’
    • ๋งŒ์•ฝ Promise๊ฐ€ reject๋˜๋ฉด, await ๋ฌธ์€ reject๋œ ๊ฐ’

Node.js ๋ชจ๋“ˆ ์‚ฌ์šฉ๋ฒ•

Node.js ๋‚ด์žฅ ๋ชจ๋“ˆ (Node.js built-in modules)์—๋Š” ๋‹ค์–‘ํ•œ ๋ชจ๋“ˆ์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์ฝ๊ฑฐ๋‚˜ ์ €์žฅํ•˜๋Š” ๊ธฐ๋Šฅ ๋“ฑ์„ ์ œ๊ณตํ•˜๋Š” Node.js ๋‚ด์žฅ ๋ชจ๋“ˆ์€

File System

๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค.

ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ๋Š”

readFile

๋ฉ”์„œ๋“œ, ํŒŒ์ผ์„ ์ €์žฅํ•  ๋•Œ๋Š”

writeFile

Node.js ์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ƒ๋‹จ์— require ๊ตฌ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const fs = require('fs'); //ํŒŒ์ผ ์‹œ์Šคํ…œ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.
const dns = require('dns'); // DNS ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

fs.readFile(path[, options], callback)

๋น„๋™๊ธฐ์ ์œผ๋กœ ํŒŒ์ผ ๋‚ด์šฉ ์ „์ฒด๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.

→ ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ์—๋Š” ์ „๋‹ฌ์ธ์ž ์„ธ ๊ฐœ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค.

  • path \<string> | \<Buffer> | \<URL> | \<integer>

 

path์—๋Š” ํŒŒ์ผ ์ด๋ฆ„์„ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

๋„ค ๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ํƒ€์ž…์„ ๋„˜๊ธธ ์ˆ˜ ์žˆ์ง€๋งŒ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌธ์ž์—ด(<string>)์˜ ํƒ€์ž…์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.

filename or file descriptor

 

 

๋‹ค์Œ์€ `'etc/passwd' ๋ผ๋Š” ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

fs.readFile('/etc/passwd', ..., ...)
  • options \<Object> | \<string>

๋Œ€๊ด„ํ˜ธ๋กœ ๊ฐ์‹ผ ๋‘ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž options๋Š” ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋„ฃ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ€๊ด„ํ˜ธ๋Š” ์„ ํƒ์  ์ „๋‹ฌ์ธ์ž๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

options๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ ์ธ์ฝ”๋”ฉ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค. ๋ฐ‘์˜ ์˜ˆ์ œ์—์„œ๋Š” 'utf8' ์„ ๋‘ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž๋กœ ๋ฐ›๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

options๋ฅผ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๋ฐ›๋Š” ๊ฒƒ์€ ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

If options is a string, then it specifies the encoding: →

// /etc/passwd ํŒŒ์ผ์„ 'utf8'์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ์Šต๋‹ˆ๋‹ค.
fs.readFile('/etc/passwd', 'utf8', ...);

[์ฝ”๋“œ] ๋‘ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž options์— ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•œ ๊ฒฝ์šฐ

let options = {
  encoding: 'utf8', // utf8 ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์œผ๋กœ ์—ฝ๋‹ˆ๋‹ค
  flag: 'r' // ์ฝ๊ธฐ ์œ„ํ•ด ์—ฝ๋‹ˆ๋‹ค
}

// /etc/passwd ํŒŒ์ผ์„ options๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ์Šต๋‹ˆ๋‹ค.
fs.readFile('/etc/passwd', options, ...)

[์ฝ”๋“œ] ๋‘ ๋ฒˆ์งธ ์ „๋‹ฌ์ธ์ž options์— ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ ๊ฒฝ์šฐ

  • callback \<Function>
    • err \<Error> | \<AggregateError>
    • data \<string> | \<Buffer>

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ํŒŒ์ผ์„ ์ฝ๊ณ  ๋‚œ ํ›„์— ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

์ฝœ๋ฐฑ ํ•จ์ˆ˜์—๋Š” ๋‘ ๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด err ๋Š” null ์ด ๋˜๋ฉฐ,

data ์— ๋ฌธ์ž์—ด์ด๋‚˜ Buffer ๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

data ๋Š” ํŒŒ์ผ์˜ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

 

์งˆ๋ฌธ: data ์—๋Š” ๋ฌธ์ž์—ด์ด๋‚˜ Buffer ๊ฐ€ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฒฝ์šฐ์— ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌ๋˜๋Š” ๊ฒƒ์ผ๊นŒ์š”? ํžŒํŠธ๋Š”, ๊ณต์‹ ๋ฌธ์„œ์˜ ๋‹ค์Œ ๋‚ด์šฉ์— ๋‚˜์™€ ์žˆ์Šต๋‹ˆ๋‹ค.

If no encoding is specified, then the raw buffer is returned. → ์ธ์ฝ”๋”ฉ์„ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์›์‹œ ๋ฒ„ํผ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.


์˜ˆ์ œ ์ฝ”๋“œ >>

JavaScript ํŒŒ์ผ์ด ์‹คํ–‰๋˜๋Š” ๋””๋ ‰ํ† ๋ฆฌ์—, ์ ๋‹นํ•œ ํ…์ŠคํŠธ ํŒŒ์ผ(test.txt)์„ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑํ•œ ํ›„ ์‹คํ–‰ํ•˜์„ธ์š”.

ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์€ ์–ด๋–ค ๊ฒƒ์ด๋“  ๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค.

const fs = require("fs"); // fs ์ถ”๊ฐ€

fs.readFile('test.txt', 'utf8', (err, data) => {
  if (err) {
    throw err; // ์—๋Ÿฌ๋ฅผ ๋˜์ง‘๋‹ˆ๋‹ค.
  }
  console.log(data);
});

// node test.js -> test.txt ๋‚ด์šฉ(asdfasdfasdf) ์ถœ๋ ฅ

// asdfasdfasdf