[Node.js] fs module


Node.js環境では、ファイルシステムモジュールを使用して「ファイルを開く」、「ファイルを保存する」などの機能を直接実現できます.
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