1. What is Node.js?
- Concept: Node.js is a JavaScript runtime built on Chrome’s V8 engine, enabling server-side JavaScript execution.
- Example:
console.log("Hello from Node.js!");
- Real Use Case: Powering backend servers for apps like Netflix, PayPal, or LinkedIn.
2. What is the Event Loop in Node.js?
- Concept: The event loop handles asynchronous operations, allowing non-blocking I/O.
- Example:
setTimeout(() => console.log("Executed later"), 0);
console.log("Executed first");
- Real Use Case: Efficient handling of thousands of requests in chat apps like WhatsApp Web.
3. What are Streams in Node.js?
- Concept: Streams let you read/write data piece by piece instead of loading it all at once.
- Example:
const fs = require('fs');
fs.createReadStream('file.txt').pipe(process.stdout);
- Real Use Case: Video/audio streaming platforms (YouTube, Spotify).
4. Difference between require and import
- Concept:
require is CommonJS; import is ES Module syntax.
- Example:
// CommonJS
const fs = require('fs');
// ES Module
import fs from 'fs';
- Real Use Case:
require for legacy Node apps, import for modern apps with ES Modules.
5. What is Middleware in Node.js (Express)?
- Concept: Functions that execute during request/response lifecycle.
- Example:
app.use((req, res, next) => {
console.log("Request Time:", Date.now());
next();
});
- Real Use Case: Logging, authentication, error handling in Express apps.
6. What are Promises & Async/Await?
- Concept: Handle asynchronous code more cleanly than callbacks.
- Example:
const fetchData = async () => {
let res = await fetch("https://api.github.com/users");
console.log(await res.json());
};
- Real Use Case: API requests in e-commerce checkout systems.
7. What is the difference between Process & Thread in Node.js?
- Concept: Process = instance of program, Thread = unit of execution inside process. Node.js is single-threaded with background worker threads.
- Example: Running one Node process with multiple requests handled asynchronously.
- Real Use Case: Scaling microservices with
cluster module.
8. Explain Node.js Clustering
- Concept: Allows running multiple Node.js processes to utilize multi-core CPUs.
- Example:
const cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
} else {
console.log("Worker process running");
}
- Real Use Case: Improving throughput in high-traffic apps (Uber, Paytm).
9. What is process.nextTick()?
- Concept: Executes callback after the current operation, before event loop continues.
- Example:
process.nextTick(() => console.log("Next Tick executed"));
console.log("Main code");
- Real Use Case: Deferring execution in async libraries like Mongoose.
10. What is an EventEmitter?
- Concept: A core module for event-driven programming.
- Example:
const EventEmitter = require('events');
const emitter = new EventEmitter();
emitter.on('start', () => console.log('Started!'));
emitter.emit('start');
- Real Use Case: Notification systems, chat events.
11. What are Worker Threads in Node.js?
- Concept: Allow running JavaScript in parallel threads.
- Example:
const { Worker } = require('worker_threads');
new Worker('./worker.js');
- Real Use Case: Heavy CPU tasks like video compression.
12. Difference between Synchronous & Asynchronous code
- Concept: Sync blocks execution, async allows parallel tasks.
- Example:
// Sync
let data = fs.readFileSync('file.txt');
// Async
fs.readFile('file.txt', (err, data) => console.log(data));
- Real Use Case: Async is critical for APIs and file systems.
13. What is JWT and how is it used in Node.js?
- Concept: JSON Web Token for authentication.
- Example:
jwt.sign({ userId: 1 }, "secretKey");
- Real Use Case: Secure login in apps like Facebook/Google sign-in.
14. What is the difference between fs.readFile and fs.createReadStream?
- Concept:
readFile loads entire file, createReadStream streams it in chunks.
- Example:
fs.readFile('big.txt', ...);
fs.createReadStream('big.txt').pipe(process.stdout);
- Real Use Case: Large file handling (logs, videos).
15. What is Nodemon?
- Concept: A tool that auto-restarts Node.js server on file changes.
- Example:
- Real Use Case: Developer productivity during backend coding.
16. What are Environment Variables in Node.js?
- Concept: Variables stored outside the code for configs.
- Example:
process.env.PORT || 3000;
- Real Use Case: Storing API keys, DB credentials.
17. Explain CORS in Node.js
- Concept: Cross-Origin Resource Sharing allows restricted resources to be accessed from another domain.
- Example:
const cors = require('cors');
app.use(cors());
- Real Use Case: Allowing frontend (React/Angular) to call backend API.
18. What is the difference between Monolithic & Microservices in Node.js?
- Concept:
- Monolithic: Single large codebase.
- Microservices: Independent services communicating via APIs.
- Example: Splitting user service, product service, payment service.
- Real Use Case: Netflix migrated from monolithic to microservices using Node.js.
19. What is Rate Limiting in Node.js?
- Concept: Restrict number of requests to avoid abuse.
- Example:
const rateLimit = require("express-rate-limit");
app.use(rateLimit({ windowMs: 60*1000, max: 5 }));
- Real Use Case: Preventing brute-force login attacks.
20. How does Node.js handle child processes?
- Concept: Allows spawning new processes.
- Example:
const { exec } = require('child_process');
exec('ls', (err, stdout) => console.log(stdout));
- Real Use Case: Running background tasks like sending emails.