Node.js Question Bank for Developers
Node.js Question Bank for Developers
The EventEmitter class in Node.js provides a core pattern for handling asynchronous events. Node.js, being event-driven, commonly uses this class to emit and handle events, where functions (listeners) are executed in response to events emitted. This enables decoupling message or event producers from consumers, promoting asynchronous behavior and facilitating complex event-driven architectures .
Synchronous functions in Node.js block the thread, waiting for operations to complete, while asynchronous functions allow other code to run, markedly enhancing responsiveness. Synchronous designs are straightforward but can lead to inefficient CPU utilization under I/O-bound scenarios. Asynchronous functions utilize callbacks, promises, or async/await patterns to optimize resource usage and maintain the application’s responsiveness even under concurrent requests, essential for non-blocking and scalable designs .
Middleware functions are used in Express.js to process requests and responses, allowing for functionalities such as logging, authentication, or handling errors. Middleware functions can modify request and response objects, execute code, end request-response cycle, or call the next middleware function in the stack. Examples include body-parser for parsing request bodies, and morgan for logging. Middleware enables modularization and reusability of code .
Node.js handles file uploads by parsing incoming files in multipart/form-data format using middleware like multer. The major steps include configuring multer to specify storage options, file size limits, and destination paths, followed by accessing uploaded files within the request object for further processing, such as saving to disk or database, while managing server-side validations and potential errors during the upload process .
Streams in Node.js provide an efficient mechanism for reading or writing data, handling it in chunks rather than loading everything into memory at once. There are various types of streams including readable, writable, duplex, and transform streams, each suited for different I/O operations. Streams enable processing of data as it arrives, allowing for faster and resource-efficient applications that utilize backpressure to handle flow appropriately. This contrasts with traditional I/O operations that can lead to high memory usage and inefficiencies .
process.nextTick() executes callbacks at the end of the current operation, before the event loop continues, while setImmediate() queues the callback for the next iteration of the event loop. This means process.nextTick() is used to execute code after the current operation ends, but before the I/O event handling starts. Choosing between them affects when in the event cycle your code will run, impacting performance and timing .
The Cluster module in Node.js allows for the creation of child processes that share the same server port, enabling Node.js to take advantage of multi-core systems and distribute requests efficiently across multiple processes. This improves the application performance by providing increased throughput and reliability, as each process can handle separate incoming connections, and the load is balanced automatically .
Best practices for securing Node.js applications include validating and sanitizing user input to avoid injection attacks, using HTTPS to encrypt communications, regularly updating dependencies to patch vulnerabilities, employing security tools like Helmet to set HTTP headers, and using environment variables for sensitive data management. These practices mitigate risks such as SQL injections, XSS, CSRF, and unauthorized data access .
REPL in Node.js (Read-Eval-Print Loop) is an interactive shell that processes Node.js JavaScript expressions, facilitating debugging and experimentation in real-time. package.json serves a different purpose; it's a manifest file in a Node.js project that contains metadata and is crucial for defining dependencies, scripts, and project information. While REPL is a developer tool for real-time evaluation, package.json manages the application environment and dependencies .
Node.js employs a non-blocking, event-driven architecture using the Event Loop to handle multiple connections concurrently on a single thread. This model, facilitated by callbacks and asynchronous processing, efficiently manages I/O operations without CPU waiting time. However, due to its single-threaded nature, it's limited in handling CPU-intensive tasks simultaneously, which may hinder performance under computationally heavy operations without adequate load-balancing, potentially addressed by the Cluster module .