Page 123 - Nodejs 교과서 개정2판
P. 123
console.log(data)ܳ ೮؊פ Bufferۄח ࢚ೠ Ѫ ۱ؾפ Ӓېࢲ EBUBী toString()ਸ ࠢৈࢲ ۽Ӓ
ܳ ନ؊פ ઁ۽ ޙৌ ۱ؾפ readFile Ѿҗޛ ߡಌ Buffer ۄח ഋधਵ۽ ઁҕؾפ Ә
ױࣽ ߡಌܳ ݫݽܻ ؘఠۄҊ ࢤпೞݶ ؾפ ীࢲ ࣁೞѱ ࢸݺפ ߡಌח ࢎۈ ੍ਸ ࣻ
ח ഋध ইפ۽ toStringਸ ࢎਊ೧ ޙৌ۽ ߸ജ೮णפ
GTח ӝࠄਵ۽ ߔ ഋध ݽٕ۽ पޖীࢲ ࢎਊೞӝо ࠛಞפ ٮۄࢲ GT ݽٕਸ ۽झ ഋधਵ۽
߄Լח ߑߨਸ ࢎਊפ
readFilePromise.js
const fs = require('fs').promises;
fs.readFile('./readme.txt')
.then((data) => {
console.log(data);
console.log(data.toString());
})
.catch((err) => {
console.error(err);
});
GT ݽٕীࢲ promise ࣘࢿਸ ࠛ۞য়ݶ ۽झ ӝ߈ GT ݽٕਸ ࢎਊೡ ࣻ ѱ ؾפ খਵ۽ח ۽झ ӝ
߈ GT ݽٕਸ ࢎਊೞѷणפ
ߣীח ੌਸ ٜ݅যࠁѷणפ
writeFile.js
const fs = require('fs').promises;
fs.writeFile('./writeme.txt', '글이 입력됩니다')
.then(() => {
return fs.readFile('./writeme.txt');
})
.then((data) => {
console.log(data.toString());
})
.catch((err) => {
console.error(err);
});
writeFile ݫࢲ٘ী ࢤࢿؼ ੌ ҃۽৬ ղਊਸ ੑ۱פ بী ী۞о ߊࢤೞ ঋওݶ э ಫ؊ ղ
ী writeme txtо ࢤࢿغਸ Ѫੑפ ৌযࠊب غ݅ ੌ दझమਸ ҕࠗೞח ۽ readFile ݫ