Complete_NodeJS_Questions_And_Answers
Complete_NodeJS_Questions_And_Answers
1. What is Node.js?
Node.js is a JavaScript runtime built on Chrome's V8 engine. It allows you to run JavaScript on the
server-side. Node.js is designed for building scalable network applications using an event-driven,
non-blocking I/O model. It excels at handling many simultaneous connections efficiently.
- Microservices
- Streaming apps
It's not ideal for CPU-heavy tasks due to its single-threaded nature.
The event loop is the core of Node.js's asynchronous architecture. It manages the execution of
multiple tasks without blocking the main thread. The event loop handles callbacks, promises, timers,
and I/O operations in a loop that picks tasks from the event queue and executes them when ready.
Promise.all runs multiple promises in parallel and fails fast if one rejects. Use it when all must
succeed.
Promise.allSettled waits for all to complete regardless of success/failure, returning their status and
value or reason. Use it when you can tolerate some failures.
A REST API is a web API that follows the principles of REST. It uses HTTP methods (GET, POST,
PUT, DELETE) to interact with resources identified by URLs. REST APIs are stateless, scalable,
and often return data in JSON format.
6. What is synchronous and asynchronous behavior in Node.js?
Synchronous operations block execution until they finish. Asynchronous operations (Node.js default)
use callbacks, promises, or async/await to allow other tasks to run while waiting. This makes
Node.js efficient for I/O-heavy operations.
The cluster module allows Node.js to create child processes (workers) that share the same server
port. It enables multi-core CPU usage and improves performance by running multiple instances of
the app in parallel.
Node.js is non-blocking due to its event-driven architecture. It uses an event loop and async APIs to
handle tasks without waiting. It immediately moves on to the next task and executes the result via
callback/promise when ready.
Use a .env file to store secrets and access them via process.env. Install the dotenv package to load
them. Add .env to .gitignore to prevent committing secrets. This keeps your credentials secure and
configurable without hardcoding.