Dosya Sistemi ve Akışlarla Çalışma
fs modülünü kullanarak Node.js'te dosyaları hem eşzamanlı hem de eşzamansız biçimde okumayı ve yazmayı öğrenin; akışların büyük miktarda veriyi verimli şekilde işlemenizi nasıl sağladığını keşfedin.
Dosya Sistemi ve Akışlarla Çalışma, CoddyKit'te ücretsiz bir Node.js Backend Development Bootcamp dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Node.js Backend Development Bootcamp öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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.
Yapay zeka eğitmeniyle JavaScript öğren — ücretsiz
Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.
- Kurslar
- 22
- Dersler
- 92
Sıkça Sorulan Sorular
“Dosya Sistemi ve Akışlarla Çalışma” dersi ücretsiz mi?
Evet — “Dosya Sistemi ve Akışlarla Çalışma” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Node.js Backend Development Bootcamp kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Node.js Backend Development Bootcamp kursu toplamda 4 dersten oluşur.
“Dosya Sistemi ve Akışlarla Çalışma” dersinde ne öğreneceğim?
fs modülünü kullanarak Node.js'te dosyaları hem eşzamanlı hem de eşzamansız biçimde okumayı ve yazmayı öğrenin; akışların büyük miktarda veriyi verimli şekilde işlemenizi nasıl sağladığını keşfedin. Node.js Backend Development Bootcamp ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Node.js Backend Development Bootcamp öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Node.js Backend Development Bootcamp, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Dosya Sistemi ve Akışlarla Çalışma” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Node.js Backend Development Bootcamp dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Node.js Backend Development Bootcamp dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Node.js ve NPM'ye Giriş
- Node.js Modül Sistemi Açıklaması
- Eşzamansız JavaScript ve Olay Döngüsü
- Dosya Sistemi ve Akışlarla Çalışma