๐ŸŒฑ → ๐ŸŒณ

[node.js] File-System(promise, async/await) ๋ณธ๋ฌธ

Server/Node.js

[node.js] File-System(promise, async/await)

BAY 2022. 9. 24. 09:02
728x90

๐Ÿ“Œ file-system

: node์˜ ๋ชจ๋“ˆ์ด๋ผ commonJS ๋ฐฉ์‹์œผ๋กœ ๋ถˆ๋Ÿฌ ์˜ฌ ์ˆ˜ ์žˆ์Œ 

const fs = require('fs');

โœ… readFile

: ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ  

 

ํŒŒ์ผ์„ ์ฝ๋Š” ๊ฒƒ๋„ ์‹œ๊ฐ„์ด ํ•„์š”ํ•œ ์ž‘์—…์ด๋ฏ€๋กœ -> callback ์‚ฌ์šฉ

fs.readFile('ํŒŒ์ผ์œ„์น˜','์œ ๋‹ˆ์ฝ”๋“œํฌ๋งท',callback(err, data) {})

err : ํŒŒ์ผ ์ฝ๊ธฐ๊ฐ€ ์ž˜ ์•ˆ๋˜์—ˆ์„ ๋•Œ, error ์ฝ”๋“œ ๋ฐ˜ํ™˜ 

data: ํŒŒ์ผ ์ฝ๊ธฐ๊ฐ€ ์ž˜ ๋˜์—ˆ์„ ๋•Œ, ์ฝ์€ data ๋ฐ˜ํ™˜

const fs = require('fs');
fs.readFile('readme.txt', 'utf-8', (err, data) => {
  if (err) {
    console.log(err);
  } else {
    console.log(data);
  }
});

 

โœ… writeFile

: ํŒŒ์ผ์„ ์ฝ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ

fs.writeFile('ํŒŒ์ผ์œ„์น˜, data, '์œ ๋‹ˆ์ฝ”๋“œํฌ๋งท, callback(err) {})

writeFile์€ data๋Š” ์—†๊ณ , ์“ฐ๊ธฐ๊ฐ€ ์ž˜ ์•ˆ๋˜์—ˆ์„ ๋•Œ err ๋ฐ˜ํ™˜

const fs = require('fs');
const str = 'ํŒŒ์ผ ์“ฐ๊ธฐ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธ ํ•ฉ๋‹ˆ๋‹ค.';
fs.writeFile('readme.txt', str, 'utf-8', (err) => {
  if (err) {
    console.log(err);
  } else {
    console.log('writefile succeed');
  }
});

 

๐Ÿ“Œ file-system with Promise

  • callback ๋ฐฉ์‹์€ ์‚ฌ์šฉ ๋ฐ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ์ด ๋–จ์–ด์ง€๊ธฐ ๋•Œ๋ฌธ์— FS๋Š” ์• ์ดˆ์— promise๋ฐฉ์‹์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณต
  • ๊ธฐ์กด ๋ฉ”์†Œ๋“œ๋ช… ๋’ค์— sync๋ฅผ ๋ถ™์ด๋ฉด promise ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์—ฌ ๋™๊ธฐ์ ์œผ๋กœ ์ž‘๋™
  • ๋‹จ, sync๊ฐ€ ๋ถ™์€ ๋ฉ”์†Œ๋“œ๋Š” ๋” ์ด์ƒ callback ํ•จ์ˆ˜๊ฐ€ ํ•„์š” ์—†์œผ๋ฏ€๋กœ ์•ˆ์จ์ค˜๋„๋จ

๐Ÿ“ 1,2,3,4๋ฒˆ์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์กด callback ๋ฐฉ์‹ ์ฝ”๋“œ

const fs = require('fs');
fs.readFile('./readme.txt', (err, data) => {
  if (err) {
    throw err;
  }
  console.log('1๋ฒˆ', data.toString());
  fs.readFile('./readme.txt', (err, data) => {
    if (err) {
      throw err;
    }
    console.log('2๋ฒˆ', data.toString());
    fs.readFile('./readme.txt', (err, data) => {
      if (err) {
        throw err;
      }
      console.log('3๋ฒˆ', data.toString());
      fs.readFile('./readme.txt', (err, data) => {
        if (err) {
          throw err;
        }
        console.log('4๋ฒˆ', data.toString());
      });
    });
  });
});

 

๐Ÿ“promise ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„

const fs = require('fs');

let data = fs.readFileSync('./readme.txt');
console.log('1๋ฒˆ', data.toString());
data = fs.readFileSync('./readme.txt');
console.log('2๋ฒˆ', data.toString());
data = fs.readFileSync('./readme.txt');
console.log('3๋ฒˆ', data.toString());
data = fs.readFileSync('./readme.txt');
console.log('4๋ฒˆ', data.toString());

 

๐Ÿ”ซ ํ•˜์ง€๋งŒ, sync๊ฐ€ ๋ถ™์€ ์ฝ”๋“œ๋Š” ์„œ๋ฒ„ ์ƒ์—์„œ ์ž‘๋™ ํ•  ๋•Œ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋นˆ๋ฒˆ 

-> fs ์“ธ ๋•Œ๋Š” fs.promise๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ•จ 

fs.promise ์Šค์Šค๋กœ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜์—ฌ resolve, reject๋ฅผ ๋ฐ˜ํ™˜

 

๐Ÿ“Œ promise 

promise๋Š” ์ผ์ข…์˜ class ์ด๋ฏ€๋กœ new ์‚ฌ์šฉ 

const promise = new Promise(function(resolve, reject) {});
  • resolve, reject๋ผ๋Š” 2๊ฐœ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฝœ๋ฐฑํ•จ์ˆ˜๋กœ ๋ฐ›์•„์„œ ์‚ฌ์šฉ
  • promise๊ฐ€ ํ• ๋‹น๋˜๋ฉด ์ด promise๋Š” resolve ๋˜๋Š” reject ํ•จ์ˆ˜๊ฐ€ callback์ด ๋  ๋•Œ ๊นŒ์ง€ ๋ฌดํ•œ ๋Œ€๊ธฐ
  • resolve: promise๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ดํ–‰ ๋˜์—ˆ์„ ๊ฒฝ์šฐ ์‚ฌ์šฉ / then์œผ๋กœ ๋ฐ›์Œ
  • reject: promise๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ์ดํ–‰๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ ์‚ฌ์šฉ / catch๋กœ ๋ฐ›์Œ
  • resolve, reject ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Œ 

resolve, reject๊ฐ€ ์‚ฌ์šฉ๋˜์ง€ ์•Š์œผ๋ฉด promise๋Š” ํ•ด๋‹น ์ฝœ๋ฐฑ์ด ๋‚˜์˜ฌ ๋•Œ ๊นŒ์ง€ pending ์ƒํƒœ๊ฐ€ ๋˜์–ด ๊ธฐ๋‹ค๋ฆผ

 

๐Ÿ“ ์œ„์˜ ๊ธฐ๋ณธ ์ฝ”๋“œ๋ฅผ fs.promise๋กœ ๋ณ€ํ™˜

const fs = require('fs').promises;
fs.readFile('./readme.txt')
  .then((data) => {
    console.log('1๋ฒˆ', data.toString());
    return fs.readFile('./readme.txt');
  })
  .then((data) => {
    console.log('2๋ฒˆ', data.toString());
    return fs.readFile('./readme.txt');
  })
  .then((data) => {
    console.log('3๋ฒˆ', data.toString());
    return fs.readFile('./readme.txt');
  })
  .then((data) => {
    console.log('4๋ฒˆ', data.toString());
  })
  .catch((err) => {
    throw err;
  });

 

๐Ÿ“Œ async/await

function ์•ž์— async๋ฅผ ๋ถ™์ด๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ Promise ๋ฐ˜ํ™˜

async๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜์—์„œ return์„ ์“ฐ๋ฉด ์•„๋ž˜์™€ ๋™์ผํ•œ ์—ญํ• ์„ ํ•จ 

async function f1() {
  return 1;
}
function f2() {
  return Promise.resolve(1);
}

async๊ฐ€ ๋ถ™์€ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—๋Š” await ์‚ฌ์šฉ ๊ฐ€๋Šฅ 

await๋Š” promise๊ฐ€ ๊ฒฐ๊ณผ(resolve, reject)๋ฅผ ๊ฐ€์ ธ๋‹ค ์ค„ ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆผ

(๋‹จ, async๋Š” ํ•จ์ˆ˜์— ๋ถ™์—ฌ์•ผ ํ•˜๋ฏ€๋กœ ๋ณ„๋„์˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ๊ธฐ๋Šฅ์„ ์ •์˜ํ•˜๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์™ธ๋ถ€์—์„œ ํ•œ๋ฒˆ ์‚ฌ์šฉํ•ด ์ค˜์•ผํ•จ)

 

๐Ÿ“์ฝ”๋“œ ์˜ˆ์‹œ

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

async function main() {
  let data = await fs.readFile('./readme.txt');
  console.log('1๋ฒˆ', data.toString());
  data = await fs.readFile('./readme.txt');
  console.log('2๋ฒˆ', data.toString());
  data = await fs.readFile('./readme.txt');
  console.log('3๋ฒˆ', data.toString());
  data = await fs.readFile('./readme.txt');
  console.log('4๋ฒˆ', data.toString());
}

main();

 

๐Ÿ“Œ routing

: ๋„คํŠธ์›Œํฌ ์ƒ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ผ ๋•Œ ์ตœ์ ์˜ ๊ฒฝ๋กœ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ณผ์ • 

 

โœ… file-system์œผ๋กœ DB๊ตฌํ˜„

database.json ํŒŒ์ผ ์ƒ์„ฑ ํ›„ ์ฝ”๋“œ ์ž‘์„ฑ

getPosts() : database.json์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์„œ ๋ณ€์ˆ˜๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜

savePosts(): ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ database.json์— ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜

const fs = require('fs');

async function getPosts() {
  const jsonPosts = await fs.promises.readFile('database.json', 'utf-8');
  return JSON.parse(jsonPosts).posts;
}

async function savePosts(posts) {
  const content = {
    posts,
  };
    return fs.promises.writeFile('database.json’, JSON.stringify(content), 'utf-8');
}

 

 

728x90