0Pricing
Node.js Backend Development Bootcamp · درس

التعامل مع نظام الملفات والتدفقات

تعلّم كيفية قراءة الملفات وكتابتها في Node.js باستخدام الوحدة fs، بشكل متزامن وغير متزامن، واكتشف كيف تتيح لك التدفقات معالجة كميات كبيرة من البيانات بكفاءة.

التعامل مع نظام الملفات والتدفقات درس مجاني في Node.js Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.

الأسئلة الشائعة

هل درس «التعامل مع نظام الملفات والتدفقات» مجاني؟

نعم — نص درس «التعامل مع نظام الملفات والتدفقات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Node.js Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة Node.js Backend Development Bootcamp 4 دروس في المجموع.

ماذا ستتعلم في «التعامل مع نظام الملفات والتدفقات»؟

تعلّم كيفية قراءة الملفات وكتابتها في Node.js باستخدام الوحدة fs، بشكل متزامن وغير متزامن، واكتشف كيف تتيح لك التدفقات معالجة كميات كبيرة من البيانات بكفاءة. تتمرن على Node.js Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى Node.js وNPM
  2. شرح نظام الوحدات في Node.js
  3. JavaScript غير المتزامنة وحلقة الأحداث
  4. التعامل مع نظام الملفات والتدفقات
← العودة إلى Node.js Backend Development Bootcamp