728x90
const fs = require('fs')
--> ํ์ผ ์์คํ ์ด๋
synchronous
: ๋๊ธฐ์ ์ฝ๋ = blocking code
ํ์ค์ด ๋๋์ผ ๋ค์์ค์ด ์คํ๋จ
Single Thread : machine processor
one application : accessing one thread
์๋ฌด๋ฆฌ ๋ง์ ์ฌ๋๋ค์ด์ฌ๋, ๊ฐ์ application์ ๋ค๋ฃจ๊ฒ๋๋ฉด, ๋ชจ๋ ์ฌ๋์ด ๋ค๋ฅธ์ฌ๋๋ค์ ์คํ์ ๊ธฐ๋ค๋ ธ๋ค๊ฐ ์ฌ์ฉ๊ฐ๋ฅํจ
const textInput = fs.readFileSync('./txt/input.txt', 'utf-8');
console.log(textInput);
const textOutput = `This is what we know about the avocado: ${textInput}.\nCreated on ${Date.now()}`
fs.writeFileSync('./txt/output.txt', textOutput)
console.log('File written!');
Non-blocking , asynchronous way
asynchronous : non-blocking code
background์์ ์คํ๋จ
fs.readFile('./txt/start.txt', 'utf-8', (err, data1) => {
if (err) return console.log('EROOR!')
fs.readFile(`./txt/${data1}.txt`, 'utf-8', (err, data2) => {
console.log(data2);
fs.readFile("./txt/append.txt", 'utf-8', (err, data3) => {
console.log("This is data3",data3);
fs.writeFile('./txt/final.txt',`${data2}\n${data3}`, 'utf-8', err => {
console.log('Your file has been written');
})
});
});
});
728x90
'Server๐งค > Node.JS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Node.js] ์ด๋ฒคํธ๊ธฐ๋ฐ (1) | 2022.10.03 |
---|---|
ํธ์ด์คํ ์ด๋? / var๋ฅผ ์ง์ํ๊ณ const, let ์ฌ์ฉํ๋ผ๋ ์ด์ (0) | 2022.10.03 |
[Node.js] NPM ๋ผ์ด๋ธ๋ฌ๋ฆฌ (0) | 2022.06.21 |
[Node.js] NPM ๋ชจ๋ (0) | 2022.06.12 |
Node.js ์ญ์ฌ , ์ฅ๋จ์ (0) | 2022.06.08 |