การทำงานกับระบบไฟล์และสตรีม
เรียนรู้การอ่านและเขียนไฟล์ใน Node.js โดยใช้มอดูล fs ทั้งแบบทำงานพร้อมกันและแบบอะซิงโครนัส พร้อมค้นพบว่าสตรีมช่วยประมวลผลข้อมูลปริมาณมากได้อย่างมีประสิทธิภาพอย่างไร
การทำงานกับระบบไฟล์และสตรีม เป็นบทเรียน Node.js Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Node.js Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why the File System Matters
Most backends eventually read or write files on disk — logs, config, uploads, reports. Node's built-in fs module handles it all; just require it.
const fs = require('fs');
console.log(typeof fs.readFile);Synchronous vs Asynchronous
Most fs methods come in two flavors: sync (like readFileSync) blocks the event loop, async doesn't. On servers, always prefer async.
const fs = require('fs');
const data = fs.readFileSync('config.txt', 'utf8');
console.log(data);Reading a File Asynchronously
The classic callback form gives you error first, then data. Always check err before touching the data — a missing file or bad permission lands there.
const fs = require('fs');
fs.readFile('notes.txt', 'utf8', (err, data) => {
if (err) { console.error('Failed:', err.message); return; }
console.log('File says:', data);
});The Promise-Based API
Modern Node exposes a promise-based API under fs/promises — pairs perfectly with async/await and skips callback nesting entirely.
const fs = require('fs/promises');
async function load() {
const data = await fs.readFile('notes.txt', 'utf8');
console.log(data);
}
load();Writing Files
Use writeFile to create or overwrite a file. No file? It's created. File exists? Its contents are replaced wholesale.
const fs = require('fs/promises');
async function save() {
await fs.writeFile('output.txt', 'Hello from Node!');
console.log('Saved.');
}
save();Appending Instead of Overwriting
To add to a file without wiping it, use appendFile — exactly what you want for log files that grow line by line.
const fs = require('fs/promises');
async function log(line) {
await fs.appendFile('app.log', line + '\n');
}
log('Server started');Working with Paths
Hard-coded paths break across operating systems. The path module fixes that: path.join uses the right separator, and __dirname is the current file's folder.
const path = require('path');
const full = path.join(__dirname, 'data', 'users.json');
console.log(full);Checking If a File Exists
Don't check existence first — that risks race conditions. Just try the operation and catch the error; a missing file throws with code ENOENT.
const fs = require('fs/promises');
async function read() {
try {
return await fs.readFile('maybe.txt', 'utf8');
} catch (err) {
if (err.code === 'ENOENT') return 'default content';
throw err;
}
}Why Streams Exist
Reading a 2 GB file with readFile loads it all into memory — a crash waiting to happen. Streams process data in small chunks, keeping memory low.
const fs = require('fs');
const stream = fs.createReadStream('huge.log', 'utf8');
stream.on('data', chunk => console.log('Got', chunk.length, 'bytes'));Piping Streams Together
The power move with streams is pipe: connect a readable straight to a writable. Node handles backpressure so a fast reader won't flood a slow writer.
const fs = require('fs');
const read = fs.createReadStream('input.txt');
const write = fs.createWriteStream('copy.txt');
read.pipe(write);Listing and Creating Directories
The fs module manages folders too: mkdir creates directories (add { recursive: true } for nested paths), and readdir lists contents.
const fs = require('fs/promises');
async function setup() {
await fs.mkdir('uploads', { recursive: true });
const files = await fs.readdir('.');
console.log(files);
}
setup();Quick Check
Test your understanding of file operations.
Recap
You can now work the file system: fs's sync/callback/promise APIs, async on servers, basic I/O, cross-platform path, and streams for big files.
คำถามที่พบบ่อย
บทเรียน “การทำงานกับระบบไฟล์และสตรีม” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การทำงานกับระบบไฟล์และสตรีม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Node.js Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Node.js Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การทำงานกับระบบไฟล์และสตรีม”
เรียนรู้การอ่านและเขียนไฟล์ใน Node.js โดยใช้มอดูล fs ทั้งแบบทำงานพร้อมกันและแบบอะซิงโครนัส พร้อมค้นพบว่าสตรีมช่วยประมวลผลข้อมูลปริมาณมากได้อย่างมีประสิทธิภาพอย่างไร คุณปฏิบัติ Node.js Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Node.js Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Node.js Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การทำงานกับระบบไฟล์และสตรีม” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Node.js Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน Node.js Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รู้จัก Node.js และ NPM
- อธิบายระบบโมดูลของ Node.js
- JavaScript แบบอะซิงโครนัสและลูปเหตุการณ์
- การทำงานกับระบบไฟล์และสตรีม