[TIL] Day25 #Node.js #fs module

Beanxxยท2022๋…„ 5์›” 30์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
25/120
post-thumbnail

[TIL] Day25
[SEB FE] Day25

โ˜‘๏ธย Node.js

๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ JS ๋Ÿฐํƒ€์ž„

๐Ÿ“Žย Node.js ๋‚ด์žฅ ๋ชจ๋“ˆ(built-in module)

  • fs.readFile: ๋กœ์ปฌ์— ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์˜ ๋‚ด์šฉ ์ „์ฒด๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์ฝ์–ด์˜ด
    • ์ „๋‹ฌ์ธ์ž 3๊ฐœ๋ฅผ ๋ฐ›์Œ (path[, options], callback)
      • path: ํŒŒ์ผ ์ด๋ฆ„ (4๊ฐ€์ง€ ํƒ€์ž… ๊ฐ€๋Šฅ - <string> | <Buffer> | <URL> | <integer>
        fs.readFile('/etc/passwd', ..., ...)
      • options: ๋„ฃ์„ ์ˆ˜๋„, ๋„ฃ์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ์Œ (๋ฌธ์ž์—ด | ๊ฐ์ฒด ํ˜•ํƒœ ๊ฐ€๋Šฅ)
        • ๋ฌธ์ž์—ด๋กœ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ ์ธ์ฝ”๋”ฉ์„ ๋ฐ›์Œ
          let options ={
          	encoding: 'utf-8',  // utf-8 ์ธ์ฝ”๋”ฉ ๋ฐฉ์‹์œผ๋กœ
          	flag: 'r' // ์ฝ๊ธฐ
          }
          
          fs.readFile('/etc/passwd', options, ...)
      • callback: ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ์ „๋‹ฌ. ํŒŒ์ผ์„ ์ฝ๊ณ  ๋‚œ ํ›„์— ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜
        • 2๊ฐ€์ง€ ๋งค๊ฐœ๋ณ€์ˆ˜ ์กด์žฌ(err | data)
          • err: ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด err = null

          • data: ํŒŒ์ผ ๋‚ด์šฉ (๋ฌธ์ž์—ด | Buffer ๊ฐ์ฒด๊ฐ€ ์ „๋‹ฌ)

            fs.readFile('test.txt', 'utf8', (err, data) => {
            	if (err) {
            		throw err;
            	}
            	console.log(data);
            });
  • fs.writeFile: ํŒŒ์ผ ์ €์žฅํ•  ๋•Œ
    const fs = require('fs');  // file system module(fs) ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
  • 3rd-party module(์„œ๋“œ ํŒŒํ‹ฐ ๋ชจ๋“ˆ): ํ•ด๋‹น ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” built-in ๋ชจ๋“ˆ์ด ์•„๋‹Œ ๋ชจ๋“  ์™ธ๋ถ€ ๋ชจ๋“ˆ
    // 3rd-party module download
    npm install underscore
    
    // ๋ชจ๋“ˆ ์‚ฌ์šฉ
    const _ = require('underscore');

โ˜‘๏ธย [Pair] Timer API

callback, Promise, async/await ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์„ ์ดํ•ดํ•˜๋Š” ํŽ˜์–ด ํ™œ๋™

โžฐย new Promise()๋ฅผ ํ†ตํ•ด ์ƒ์„ฑํ•œ Promise ์ธ์Šคํ„ด์Šค์— ์กด์žฌํ•˜๋Š” ๋ฉ”์„œ๋“œ

  • .then(): ํ”„๋กœ๋ฏธ์Šค ์ดํ–‰์‹œ ์ฝœ๋ฐฑ ๋ฐ˜ํ™˜
  • .catch(): ํ”„๋กœ๋ฏธ์Šค ๊ฑฐ๋ถ€์‹œ ์ฝœ๋ฐฑ ๋ฐ˜ํ™˜
  • .finally(): ํ”„๋กœ๋ฏธ์Šค์˜ ์ดํ–‰/๊ฑฐ๋ถ€ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด ์ฒ˜๋ฆฌ๋  ๊ฒฝ์šฐ ํ•ญ์ƒ ํ˜ธ์ถœ

โžฐย ํ”„๋กœ๋ฏธ์Šค 3๊ฐ€์ง€ ์ƒํƒœ

  1. Pending(๋Œ€๊ธฐ): ์ดํ–‰ํ•˜์ง€๋„, ๊ฑฐ๋ถ€ํ•˜์ง€๋„ ์•Š์€ ์ดˆ๊ธฐ ์ƒํƒœ

    new Promise();
  1. Fulfilled(์ดํ–‰): ์—ฐ์‚ฐ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒ๋œ ์ƒํƒœ

    new Promise(function(resolve, reject) {
    	resolve();
    });
  1. Rejected(๊ฑฐ๋ถ€): ์—ฐ์‚ฐ์ด ์‹คํŒจํ•œ ์ƒํƒœ

    new Promise(function(resolve, reject) {
    	reject();
    });

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


โ˜‘๏ธย [Pair] fs module

callback, Promise, async/await๋ฅผ ํ™œ์šฉํ•˜์—ฌ fs module์„ ์‚ฌ์šฉํ•ด๋ณด๋Š” ํŽ˜์–ด ํ™œ๋™

แ„‰แ…ณแ„แ…ณแ„…แ…ตแ†ซแ„‰แ…ฃแ†บ 2022-05-31 แ„‹แ…ฉแ„Œแ…ฅแ†ซ 1 43 40
  1. fs module์„ callback์„ ํ†ตํ•ด ํŒŒ์ผ ์ •๋ณด ์ฝ์–ด์˜ค๊ธฐ

    // fs module ๋ถˆ์–ด์˜ค๊ธฐ
    const fs = require('fs');
    
    const getDataFromFile = function (filePath, callback) {
    	// 'utf8' encoding ํ•˜์ง€ ์•Š์œผ๋ฉด ํ•œ๊ธ€ ๊นจ์ง ํ˜„์ƒ ๋ฐœ์ƒ
      fs.readFile(filePath, "utf8", (err, data) => {
        if (err) {  // ์—๋Ÿฌ๋‚œ ๊ฒฝ์šฐ
          callback(err, null); // ์—๋Ÿฌ ๋ถˆ๋Ÿฌ์˜ด
        } else {    // ์—๋Ÿฌ๋‚˜์ง€ ์•Š์€ ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ
          callback(null, data);  // ํŒŒ์ผ ์ •๋ณด ์ฝ์–ด์˜ค๊ธฐ
        }
      });
    };
    
    module.exports = {
      getDataFromFile,
    };
  1. fs module์„ Promise์„ ํ†ตํ•ด ํŒŒ์ผ ์ •๋ณด ์ฝ์–ด์˜ค๊ธฐ

    const fs = require("fs");
    
    const getDataFromFilePromise = (filePath) => {
      return new Promise((resolve, reject) => {
        fs.readFile(filePath, "utf8", (err, data) => {
          if (err) {
            reject(err);
          } else {  // ์—๋Ÿฌ๊ฐ€ ๋‚˜์ง€ ์•Š์€ ๊ฒฝ์šฐ resolve ๋ฉ”์†Œ๋“œ ์‹คํ–‰ -> ํŒŒ์ผ ์ •๋ณด ์ฝ์–ด์˜ด
            resolve(data);
          }
        });
      });
    };
  1. 2๊ฐœ์˜ json ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ ํ›„, ํ•ฉ์ณ์„œ ๋‘ ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด ์ถœ๋ ฅ

    const path = require("path");
    const { getDataFromFilePromise } = require("./02_promiseConstructor");
    
    const user1Path = path.join(__dirname, "files/user1.json");
    const user2Path = path.join(__dirname, "files/user2.json");
    
    const readAllUsersChaining = () => {
      let result = [];  // ๋นˆ ๋ฐฐ์—ด ์„ ์–ธ
    
      return getDataFromFilePromise(user1Path)  // ์ฒซ๋ฒˆ์งธ ํŒŒ์ผ ์ •๋ณด ๊ฐ€์ ธ์™€์„œ
        .then((data) => {
          let data1 = JSON.parse(data);  // json ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
          result.push(data1);  // ๋นˆ ๋ฐฐ๋ณ„์— ์ฒซ๋ฒˆ์งธ ํŒŒ์ผ ๊ฐ์ฒด ๋„ฃ์–ด์คŒ
          return getDataFromFilePromise(user2Path);  // ๋‘๋ฒˆ์งธ ํŒŒ์ผ ์ •๋ณด ๊ฐ€์ ธ์™€์„œ
        })
        .then((data) => {
          let data2 = JSON.parse(data);  // json ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
          result.push(data2);  // ์ฒซ๋ฒˆ์งธ ํŒŒ์ผ ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฐฐ์—ด์— ๋‘๋ฒˆ์งธ ํŒŒ์ผ ๊ฐ์ฒด ๋„ฃ์–ด์คŒ
          return result;  // ์ฒซ๋ฒˆ์งธ, ๋‘๋ฒˆ์งธ ํŒŒ์ผ ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ„ ๋ฐฐ์—ด ๋ฐ˜ํ™˜
        });
    };
    
    // JSON.parse() ์ „์˜ ํŒŒ์ผ ์ •๋ณด์—๋Š” key&value ๋ชจ๋‘ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Œ
    // JSON.parse() ํ›„์˜ ํŒŒ์ผ ๊ฐ์ฒด์—” key๋ฅผ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” ""์ด ์ œ๊ฑฐ๋œ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด์คŒ
  1. Promise.all() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 3๋ฒˆ์งธ ์ถœ๋ ฅ ๊ฒฐ๊ณผ์™€ ๊ฐ™๋„๋ก ๊ตฌํ˜„

    const readAllUsers = () => {
      return Promise.all([ // ์ „๋‹ฌ์ธ์ž๋ฅผ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ๋ฐ›์Œ
        getDataFromFilePromise(user1Path),
        getDataFromFilePromise(user2Path),
      ]).then((data) => data.map((el) => JSON.parse(el)));
    };

    โžฐย Promise.all()

    • ์ฃผ์–ด์ง„ ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ดํ–‰ํ•˜๊ฑฐ๋‚˜, ํ•œ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๊ฑฐ๋ถ€๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐํ•˜๋Š” ์ƒˆ๋กœ์šด ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜
    • ๋™์‹œ์— 2๊ฐœ ์ด์ƒ์˜ Promise ์š”์ฒญ์„ ํ•œ๊บผ๋ฒˆ์— ์‹คํ–‰ํ•˜๋Š” ํ•จ์ˆ˜
    • Promise.all์˜ ์ „๋‹ฌ์ธ์ž๋Š” ๋ฐฐ์—ด ํ˜•ํƒœ
    • Promise.all์˜ ์ „๋‹ฌ์ธ์ž์— ๋ฐฐ์—ด์ด ์ „๋‹ฌ๋œ๋‹ค๋ฉด, ์š”์†Œ ์ˆœ์„œ๋Š” Promise.all์˜ Promise ์ˆœ์„œ์™€ ์ƒ์‘
    • Promise.all์— ์ „๋‹ฌ๋˜๋Š” Promise๋Š” ํ•˜๋‚˜๋ผ๋„ ๊ฑฐ๋ถ€๋˜๋Š” ์ˆœ๊ฐ„ ๋ชจ๋“  ์š”์ฒญ์ด ๊ฑฐ๋ถ€๋จ
      ๐Ÿ‘‰ ๋ชจ๋“  Promise ์š”์ฒญ์ด ๊ฑฐ๋ถ€๋˜๋ฉด์„œ .catch๋ฅผ ๋”ฐ๋ผ๊ฐ.
  2. async/await ์‚ฌ์šฉํ•˜์—ฌ ์œ„์™€ ๊ฐ™์€ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ๊ตฌํ˜„

    const readAllUsersAsyncAwait = async () => {
      let result = [];
      let user1 = await getDataFromFilePromise(user1Path);
      let user2 = await getDataFromFilePromise(user2Path);
      let data1 = JSON.parse(user1);
      let data2 = JSON.parse(user2);
      result.push(data1, data2);
      return result;
    };
profile
FE developer

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