Section 1: Core & Architecture
#1 ARCHITECTURE
EN What is the Event Loop and how does it work?
The Event Loop is a mechanism that allows Node.js to perform non-blocking I/O operations despite being single-threaded. It constantly checks for events in various phases (Timers, Poll, Check) and executes their callbacks. It offloads operations to the system kernel or thread pool whenever possible.
AR ما هو الـ Event Loop وكيف يعمل؟
الـ Event Loop هو محرك Node.js الذي يسمح له بتنفيذ عمليات غير منغلقة (non-blocking) رغم كونه أحادي المسار. يقوم باستمرار بفحص الأحداث في مراحل مختلفة (مثل التوقيت، الفحص، والطلبات) وينفذ الدوال الخاصة بها. يقوم بنقل العمليات الثقيلة إلى نواة النظام أو مصفوفة المسارات (Thread Pool) عند الحاجة.
#2 MODULES
EN What is the difference between CommonJS and ESM?
**CommonJS (CJS)** is the legacy Node standard using `require()` and `module.exports`, loading modules synchronously. **ES Modules (ESM)** is the modern standard using `import` and `export`, loading modules asynchronously. ESM supports "Top-level await" and is the standard for modern browsers and Node 12+.
AR ما الفرق بين CommonJS و ES Modules؟
**CommonJS** هو النظام القديم لـ Node ويستخدم `require()`، ويحمل المكتبات بشكل متزامن. **ES Modules** هو النظام الحديث والمعياري ويستخدم `import` و `export` ويحمل المكتبات بشكل غير متزامن، ويدعم الـ "Top-level await" وهو المعتمد في المتصفحات الحديثة وإصدارات نود 12 فما فوق.
#3 BUFFERS
EN What are Buffers in Node.js?
Buffers are objects used to handle binary data. Since JavaScript originally had no way to handle raw binary data (like images or network packets), Node introduced the `Buffer` class to store raw memory outside the V8 heap.
AR ما هي الـ Buffers في Node.js؟
الـ Buffers هي كائنات تُستخدم للتعامل مع البيانات الثنائية (Binary Data). بما أن الجافا سكريبت لم تكن تدعم البيانات الخام (مثل الصور أو حزم الشبكة)، قدم Node كلاس `Buffer` لتخزين البيانات مباشرة في الذاكرة خارج نطاق الـ V8.
#4 STREAMS
EN Why should you use Streams instead of fs.readFile?
`fs.readFile` loads the entire file into memory before processing, which crashes if the file is larger than the available RAM. **Streams** process data in small chunks, using constant memory regardless of the file size, making them much faster and more scalable.
AR لماذا يجب استخدام الـ Streams بدلاً من fs.readFile؟
تقوم `fs.readFile` بتحميل الملف بالكامل في الذاكرة قبل المعالجة، مما يسبب انهيار السيرفر إذا كان الملف أكبر من الذاكرة المتاحة. أما الـ **Streams** فتقوم بمعالجة البيانات على أجزاء صغيرة (Chunks)، مما يوفر استهلاك الذاكرة ويجعل التطبيق أسرع وأكثر استقراراً.
#5 EVENTS
EN What is EventEmitter and when to use it?
`EventEmitter` is a class that allows objects to emit events and register listeners. It is used when you want to implement the Observer pattern, where one part of your application needs to notify other parts when something happens (e.g., a file upload finishing).
AR ما هو EventEmitter ومتى نستخدمه؟
هو كلاس يسمح للكائنات بإرسال أحداث (Emit Events) وتسجيل مستمعين (Listeners) لها. يُستخدم عند تنفيذ نمط المراقبة (Observer pattern)، حيث يحتاج جزء من التطبيق لإخطار أجزاء أخرى بوقوع حدث معين (مثل انتهاء رفع ملف).
Section 2: Async & Performance
#6 NEXT TICK
EN What is the difference between process.nextTick() and setImmediate()?
`process.nextTick()` executes its callback **immediately** after the current operation, before the Event Loop continues to the next phase. `setImmediate()` executes its callback in the **Check phase** of the Event Loop (after the Poll phase). `nextTick` is much higher priority.
AR ما الفرق بين process.nextTick() و setImmediate()؟
تقوم `process.nextTick()` بتنفيذ الكود **فوراً** بعد العملية الحالية وقبل انتقال الـ Event Loop للمرحلة التالية. أما `setImmediate()` فتنفذ الكود في مرحلة الـ **Check** (بعد مرحلة الـ Poll). الـ `nextTick` لها أولوية قصوى وتعمل قبل أي شيء آخر.
#7 THREADING
EN How can Node.js handle concurrent requests if it is single-threaded?
Node.js uses **Asynchronous Non-blocking I/O**. When a request involves waiting (like DB or File access), Node offloads that to the system kernel. While the kernel works, the single thread continues to process other requests. When the data is ready, the callback is sent to the queue.
AR كيف يتعامل Node مع طلبات متعددة وهو أحادي المسار؟
يعتمد نود على **المدخلات والمخرجات غير المنغلقة**. عندما يتطلب الطلب انتظاراً (مثل قاعدة البيانات)، ينقل نود هذه المهمة للنظام. بينما يعمل النظام، يستمر المسار الوحيد في استقبال طلبات أخرى. وعندما تصبح البيانات جاهزة، يتم تنفيذ الكود الخاص بها من طابور الانتظار.
#8 WORKER THREADS
EN When should you use Worker Threads?
Worker Threads should be used for **CPU-intensive tasks** (like video encoding, data processing, or complex calculations) that would otherwise block the Event Loop and make the server unresponsive.
AR متى يجب استخدام Worker Threads؟
يجب استخدامها في **العمليات التي تستهلك المعالج (CPU) بشكل مكثف** (مثل معالجة الصور أو الحسابات المعقدة). هذه العمليات قد تؤدي لتوقف الـ Event Loop، لذا نقوم بتشغيلها في مسارات منفصلة لضمان استجابة السيرفر.
#9 SCALING
EN What is the Cluster module and how does it help?
The `cluster` module allows you to create child processes (workers) that share the same server port. This allows you to utilize all available CPU cores of the server, effectively multiplying your application's capacity.
AR ما هي وحدة الـ Cluster وكيف تساعد في الأداء؟
تسمح وحدة الـ `cluster` بإنشاء عمليات فرعية (Workers) تشترك في نفس المنفذ (Port). هذا يسمح للتطبيق باستخدام جميع أنوية المعالج (CPU Cores) المتاحة في السيرفر، مما يضاعف قدرة التطبيق على تحمل الضغط.
#10 PM2
EN Why is PM2 essential for production?
PM2 is a process manager that keeps your application alive forever. If the app crashes, PM2 restarts it automatically. It also handles clustering, log management, and zero-downtime reloads.
AR لماذا يعتبر PM2 أساسياً لبيئة التشغيل (Production)؟
هو مدير عمليات يضمن بقاء التطبيق يعمل باستمرار. إذا تعطل التطبيق، يقوم PM2 بإعادة تشغيله تلقائياً. كما يدعم توزيع الأحمال (Clustering)، وإدارة السجلات (Logs)، وتحديث الكود بدون توقف السيرفر.
Section 3: Web & Frameworks
#11 MIDDLEWARE
EN Explain the order of middleware execution in Express.
Middleware executes in the order it is defined using `app.use()`. If a middleware doesn't call `next()`, the request will hang and subsequent middleware or routes will never be reached.
AR اشرح ترتيب تنفيذ الـ Middleware في Express.
يتم تنفيذ الـ Middleware حسب ترتيب كتابتها في الكود باستخدام `app.use()`. إذا لم تقم دالة وسيطة باستدعاء `next()`، سيتوقف الطلب ولن تصل الاستجابة لباقي الدوال أو المسارات.
#12 ERROR HANDLING
EN How do you handle errors in Express middleware?
Error-handling middleware is defined with four arguments instead of three: `(err, req, res, next)`. Express recognizes this signature and only calls this function when an error is passed to `next(error)`.
AR كيف تتعامل مع الأخطاء في Middleware الخاص بـ Express؟
يتم تعريف الـ Middleware الخاص بالأخطاء بأربعة بارامترات بدلاً من ثلاثة: `(err, req, res, next)`. يتعرف Express على هذا التنسيق ويقوم باستدعائه فقط عندما يتم تمرير خطأ للدالة `next(error)`.
#13 PARAMS
EN What is the difference between req.params and req.query?
`req.params` contains route parameters defined in the path (e.g., `/user/:id` -> `req.params.id`). `req.query` contains the URL query string parameters (e.g., `/search?name=john` -> `req.query.name`).
AR ما الفرق بين req.params و req.query؟
`req.params` تحتوي على متغيرات مسار الرابط (مثل `/user/:id`). أما `req.query` فتحتوي على متغيرات الاستعلام التي تأتي بعد علامة الاستفهام في الرابط (مثل `/search?name=john`).
#14 BODY PARSER
EN Why do we need express.json()?
By default, Node/Express cannot read the data sent in the request body (POST/PUT). `express.json()` is a built-in middleware that parses incoming requests with JSON payloads and populates `req.body`.
AR لماذا نحتاج لاستخدام express.json()؟
بشكل افتراضي، لا يستطيع نود قراءة البيانات المرسلة في جسم الطلب (Body). تعتبر `express.json()` برمجية وسيطة تقوم بتحليل بيانات الـ JSON القادمة ووضعها في `req.body` ليسهل التعامل معها.
#15 REPL
EN What is REPL in Node.js?
REPL stands for **Read, Eval, Print, Loop**. It is an interactive shell environment (like typing `node` in terminal) where you can execute JavaScript code line by line and see results immediately.
AR ما هو الـ REPL في Node.js؟
اختصار لـ **Read, Eval, Print, Loop**. هي بيئة تفاعلية (مثل كتابة `node` في الشاشة السوداء) تسمح لك بكتابة كود جافا سكريبت وتجربته سطراً بسطر ورؤية النتائج فوراً.
Section 4: Security & Database
#16 SECURITY
EN What is Helmet and why use it?
Helmet is a middleware that secures Express apps by setting various HTTP headers. It helps protect against common vulnerabilities like XSS, Clickjacking, and MIME-sniffing.
AR ما هو Helmet ولماذا نستخدمه؟
هو Middleware يقوم بحماية تطبيقات Express عبر ضبط ترويسات (Headers) الـ HTTP. يساعد في الحماية من ثغرات مشهورة مثل XSS و Clickjacking وغيرها.
#17 JWT
EN What is JWT and how does it work?
JSON Web Token is a secure way to transmit information between parties as a JSON object. In Node, it is used for authentication. The server generates a token after login, and the client sends it in the `Authorization` header for subsequent requests.
AR ما هو الـ JWT وكيف يعمل؟
هو وسيلة آمنة لنقل البيانات بين طرفين ككائن JSON. في نود، يُستخدم للتحقق من الهوية (Authentication). يقوم السيرفر بإنشاء "توكن" بعد تسجيل الدخول، ويرسله العميل في كل طلب لاحق لإثبات هويته.
#18 DB INJECTION
EN How do you prevent SQL/NoSQL injection?
To prevent injection, never concatenate user input directly into queries. Instead, use **Parameterized Queries** or an ORM/ODM like **Mongoose** or **Prisma**, which sanitize inputs automatically.
AR كيف تمنع هجمات الـ SQL/NoSQL Injection؟
لمنع هذه الهجمات، يجب ألا تضع مدخلات المستخدم مباشرة في الاستعلام. بدلاً من ذلك، استخدم الاستعلامات المعلمة (Parameterized Queries) أو استخدم ORM مثل **Mongoose** أو **Prisma** التي تقوم بتنظيف البيانات تلقائياً.
#19 ENV VARS
EN Why should you use environment variables?
Environment variables keep sensitive data (DB passwords, API keys) outside the source code. This improves security and allows different configurations for development, testing, and production.
AR لماذا يجب استخدام متغيرات البيئة (Env Vars)؟
لإبقاء البيانات الحساسة (كلمات سر قاعدة البيانات، مفاتيح الـ API) خارج الكود المصدري. هذا يحسن الأمان ويسمح بإعدادات مختلفة لكل بيئة (تطوير، اختبار، إنتاج).
#20 PROS/CONS
EN What are the advantages and disadvantages of Node.js?
**Pros**: Extremely fast I/O, huge NPM ecosystem, same language (JS) for frontend/backend. **Cons**: Not suitable for heavy CPU tasks, callback hell (if not handled), single-threaded limitation for some calculations.
AR ما هي مميزات وعيوب Node.js؟
**المميزات**: سرعة هائلة في المدخلات والمخرجات، نظام NPM ضخم، لغة واحدة (JS) للفرونت إند والباك إند. **العيوب**: غير مناسب للعمليات التي تستهلك المعالج بكثافة، تعقيد الكود إذا لم يُنظم جيداً، والتقيد بمسار واحد لبعض الحسابات.