0Pricing
Node.js Backend Development Bootcamp · Lektion

Arbeiten mit Dateisystem und Streams

Lernen Sie, wie Sie in Node.js mit dem fs-Modul Dateien synchron und asynchron lesen und schreiben, und entdecken Sie, wie Streams große Datenmengen effizient verarbeiten.

Arbeiten mit Dateisystem und Streams ist eine kostenlose Node.js Backend Development Bootcamp-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Node.js Backend Development Bootcamp-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Node.js Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Arbeiten mit Dateisystem und Streams“ kostenlos?

Ja — der vollständige Text von „Arbeiten mit Dateisystem und Streams“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Node.js Backend Development Bootcamp-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Node.js Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Arbeiten mit Dateisystem und Streams“?

Lernen Sie, wie Sie in Node.js mit dem fs-Modul Dateien synchron und asynchron lesen und schreiben, und entdecken Sie, wie Streams große Datenmengen effizient verarbeiten. Du übst Node.js Backend Development Bootcamp mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Node.js Backend Development Bootcamp zu starten?

Keine Vorkenntnisse erforderlich. Node.js Backend Development Bootcamp auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Arbeiten mit Dateisystem und Streams“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Node.js Backend Development Bootcamp-Lektion Code schreiben und ausführen?

Ja. Jede Node.js Backend Development Bootcamp-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in Node.js und NPM
  2. Das Node.js-Modulsystem erklärt
  3. Asynchrones JavaScript und Event Loop
  4. Arbeiten mit Dateisystem und Streams
← Zurück zu Node.js Backend Development Bootcamp