[Node.js] fs module

Steve·2021년 6월 11일

공부

목록 보기
7/10

Node.js 환경에서는 fs(File System) module 을 사용하여 "파일 열기", "파일 저장하기" 등을 직접 구현할 수 있다.
Node.js 에서는 파일을 읽는 비동기 함수를 제공한다.

https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_fs_readfile_path_options_callback

const fs = require('fs');
const path = require('path');

fs.readFile('/etc/passwd', 'utf8', (err, data) => {
  if (err) throw err;
  console.log(data);
});
// 데이터가 text 일 경우 'utf8' 옵션을 넣어준다.

const user1Path = path.join(__dirname, 'files/user1.json'); 
console.log(user1Path) // 파일의 절대경로

https://www.digitalocean.com/community/tutorials/nodejs-how-to-use__dirname

profile
배우는 가장 좋은 방법은 가르치는 것이다.

0개의 댓글