모듈이 뭐야?
건축으로부터 비롯된 모듈이라는 단어는, 어떤 기능을 조립할 수 있는 형태로 만든 부분이다. fs(File System) 모듈은, PC의 파일을 생성하거나 삭제하거나 읽거나 쓸 수 있게 도와준다.
const fs = require('fs'); // 파일 시스템 모듈을 불러옵니다
const dns = require('dns'); // DNS 모듈을 불러옵니다
// 이제 fs.readFile 메소드 등을 사용할 수 있습니다
npm install underscore
const _ = require('underscore')
// 이제 underscore 모듈의 메소드를 사용할 수 있습니다.
fs.readFile(path[, options], callback)
path
options
fs.readFile('/desktop/section2','utf8',...)
const options = {
encoding: 'utf8', // UTF-8이라는 인코딩 방식으로 엽니다
flag: 'r' // 읽기 위해 엽니다
}
fs.readFile('/desktop/section2',options,...)
callback
//readme.txt
//저를 읽어주세요
//readFile.js
const fs = require('fs');
fs.readFile('./readme.txt', (err,data)=>{
if(err){
throw err;
}
console.log(data);
console.log(data.toString());
});
//<Buffer ec a0 80 eb a5 bc 20 ec 9d bd ec 96 b4 ec a3 bc ec 84 b8 ec 9a 94 2e>
//저를 읽어주세요