- What is Node.js?
Node.js is a JavaScript runtime built on Chrome's V8 engine that allows server-side execution of JS code.
- Why is Node.js single-threaded?
Node.js uses a single-threaded event loop to handle concurrent requests efficiently without creating multiple threads.
- What is the V8 engine?
V8 is Google’s open-source JavaScript engine that compiles JS to machine code for fast execution.
- What is the Event Loop?
The Event Loop is a mechanism that handles asynchronous operations and ensures non-blocking I/O.
- Difference between Node.js and JavaScript in the browser.
Node.js runs JS on the server, while browser JS runs on the client. Node.js provides file system, network APIs which browser JS lacks.
- What are global objects in Node.js?
Objects accessible in all modules without requiring them, e.g., `global`, `process`, `__dirname`, `__filename`.
- What is npm?
Node Package Manager, used to install, share, and manage Node.js packages and dependencies.
- What is package.json?
A JSON file containing project metadata, dependencies, scripts, and configuration for a Node.js project.
- What is module.exports?
An object used to export functions, objects, or variables from a module to be used in other files.
- Difference between require and import.
`require` is used in CommonJS modules, `import` is used in ES6 modules.
- What is callback?
A function passed as an argument to be executed after another function completes.
- What is asynchronous programming?
Executing tasks without blocking the main thread, allowing other operations to run concurrently.
- What is Promise?
An object representing the eventual completion or failure of an asynchronous operation.
- Difference between Promise and callback.
Promises allow chaining and better error handling compared to nested callbacks.
- What is async/await?
Syntactic sugar to write asynchronous code in a synchronous style using Promises.
- What is REPL?
Read-Eval-Print Loop, an interactive shell to execute Node.js commands line by line.
- What is middleware?
Functions in frameworks like Express.js that process requests before reaching the final route handler.
- What is Express.js?
A minimal and flexible Node.js web framework for building APIs and web applications.
- What is REST API?
An API design style using HTTP requests for communication between client and server with stateless operations.
- How do you handle errors in Node.js?
Using try/catch for synchronous code, callbacks with error arguments, or Promises/async-await error handling.
- What is non-blocking I/O?
I/O operations that don’t block the execution thread, allowing other code to run while waiting for I/O completion.
- How does the Event Loop work internally?
It continuously checks the event queue and executes callbacks, managing timers, I/O, and idle operations.
- Difference between process.nextTick and setImmediate.
`process.nextTick` executes before the next event loop tick, `setImmediate` executes on the following tick.
- What are streams in Node.js?
Streams are objects to read or write data in chunks instead of loading everything into memory.
- Types of streams.
Readable, Writable, Duplex, and Transform streams.
- What is Buffer?
A temporary storage for binary data in Node.js.
- What is clustering?
Creating multiple Node.js processes to utilize multiple CPU cores for better performance.
- How do you handle multiple requests?
Node.js handles requests asynchronously using the event loop and non-blocking I/O.
- How do you manage environment variables?
Using `.env` files with packages like `dotenv` or through OS environment variables.
- What is CORS?
Cross-Origin Resource Sharing, a security feature controlling which domains can access resources.
- How do you secure Node.js APIs?
Use authentication, validation, HTTPS, helmet, and proper error handling.
- What is JWT authentication?
JSON Web Tokens used for stateless authentication between client and server.
- How do you connect Node.js with databases?
Using drivers or ORMs like `mongoose` for MongoDB or `sequelize` for SQL databases.
- Difference between SQL and NoSQL in Node.js apps.
SQL is relational and structured, NoSQL is non-relational and flexible for unstructured data.
- What is Mongoose?
An ODM (Object Data Modeling) library for MongoDB and Node.js.
- How do you handle file uploads?
Using middleware like `multer` to process multipart/form-data.
- What is rate limiting?
Restricting the number of requests a client can make to prevent abuse.
- How do you implement pagination?
Using query parameters like `limit` and `offset` to fetch partial data from the database.
- How do you handle logging?
Using packages like `winston` or `morgan` to log requests, errors, and system events.
- How do you test Node.js applications?
Using frameworks like `mocha`, `jest`, or `chai` for unit, integration, and end-to-end testing.
- Explain Node.js architecture.
Node.js uses a single-threaded event loop with non-blocking I/O and a libuv library for asynchronous operations.
- How do you scale Node.js applications?
By clustering, load balancing, horizontal scaling, and microservices architecture.
- What is horizontal vs vertical scaling?
Horizontal scaling adds more servers, vertical scaling increases resources of a single server.
- How do you handle CPU-intensive tasks?
Offload to worker threads, separate services, or queue jobs using message queues.
- How do you design microservices in Node.js?
Divide app into smaller services communicating via HTTP, gRPC, or message queues, each service independent and deployable.
- How do you handle inter-service communication?
Through REST, gRPC, or message queues like RabbitMQ or Kafka.
- What is message queue usage in Node.js?
Queues decouple services, manage asynchronous tasks, and improve scalability.
- How do you implement caching?
Using Redis or in-memory caches to reduce database load and improve response time.
- What is Redis used for?
In-memory data structure store for caching, pub/sub, and fast data access.
- How do you handle memory leaks?
Use profiling tools, monitor memory usage, and fix code that keeps unnecessary references.
- How do you monitor Node.js applications?
Using monitoring tools like PM2, New Relic, or Datadog to track performance and errors.
- How do you implement CI/CD for Node.js?
Automate testing, building, and deployment using pipelines like GitHub Actions, Jenkins, or GitLab CI.
- How do you secure APIs at enterprise level?
Use authentication, encryption, rate limiting, input validation, and regular security audits.
- How do you handle API versioning?
Use URL versioning (`/v1/endpoint`), headers, or query parameters to maintain backward compatibility.
- How do you manage secrets?
Store in environment variables, secret managers, or encrypted configuration files.
- What are Node.js performance bottlenecks?
Blocking I/O, CPU-intensive tasks, memory leaks, and unoptimized database queries.
- How do you implement graceful shutdown?
Listen for termination signals, close connections, finish ongoing requests, then exit.
- What are common Node.js anti-patterns?
Blocking I/O, callback hell, overusing global variables, and improper error handling.
- How do you design fault-tolerant systems?
Use redundancy, retries, fallbacks, monitoring, and isolation to minimize failures.
- When should you NOT use Node.js?
For CPU-intensive tasks, heavy computation apps, or applications needing multi-threaded processing.