Node.js MCQs: 25 Essential Questions
Node.js MCQs: 25 Essential Questions
Despite Node.js’s single-thread model, concurrency is achieved by using the ‘child_process’ module which allows the running of JavaScript operations concurrently via sub-processing. Child processes can be created using methods like 'spawn', 'exec', or 'fork'. These processes can perform tasks asynchronously in parallel with the main execution thread, leveraging multitasking without blocking the main thread, thus enhancing performance and scalability .
The core difference between using the 'http' and 'ws' modules in Node.js lies in their protocol support. The 'http' module is used for creating HTTP servers which handle requests over the HTTP/HTTPS protocols, suitable for traditional web services. In contrast, the 'ws' module is used to create WebSocket servers which facilitate bi-directional full-duplex communication channels over a single TCP connection, ideal for real-time applications like chat applications .
The 'require' function in Node.js is used for module management and contributes by importing modules, JSON, or local files into the application. It reads a JavaScript file, executes it, and then returns the module's exports object. This mechanism allows for code modularization, enabling code separation and reuse across different parts of an application .
The 'path' module in Node.js is used for working with file and directory paths. It provides utilities that simplify the manipulation and creation of paths across different operating systems by normalizing paths and resolving relative paths to absolute paths. Functions from this module, such as 'path.join', 'path.resolve', and 'path.basename', are vital in building cross-platform file systems by ensuring consistent path transformation and concatenation .
npm, Node Package Manager, is a critical component of the Node.js ecosystem. It is used to manage and install packages/modules from the npm registry. This functionality is significant as it allows developers to easily share and reuse code, thus fostering a large community of modules contributing to rapid development and innovation within the Node.js ecosystem .
The 'http' module in Node.js provides the ability to transfer data over the Hyper Text Transfer Protocol (HTTP). It facilitates server creation by providing functions that create server instances capable of listening to ports and handling incoming HTTP requests. The typical use case involves using the 'http.createServer' method to start an HTTP server, enabling the handling of HTTP requests and responses seamlessly .
A developer might choose to install a package globally in Node.js when they need the package to be available across multiple projects, or when using command-line tools provided by the package. The command used for global installation is 'npm install -g package-name', which installs the package globally, making it accessible from the command line interface regardless of the current project directory .
'fs.readFile' is an asynchronous method used to read the contents of a file, meaning it doesn't block the execution thread while the file is being read. In contrast, 'fs.readFileSync' is synchronous and blocks the thread until the file is entirely read. The asynchronous method ('fs.readFile') is preferred for its non-blocking performance, ideal for scalable applications, whereas 'fs.readFileSync' can lead to performance bottlenecks due to its blocking nature .
Node.js manages multiple I/O requests simultaneously using an asynchronous event-driven architecture rather than multi-threading. This is supported by the Node.js single-threaded event loop that allows non-blocking I/O operations. As a result, Node.js can process other tasks while waiting for I/O operations to complete, effectively managing multiple requests in parallel .
Node.js ensures security in buffer management by carefully managing memory allocation and providing APIs that prevent common vulnerabilities such as buffer overflows. The 'Buffer' class in Node.js safely handles raw binary data based on the underlying memory buffer, with built-in safeguards to prevent memory breaches, such as boundary checks to keep operations within limits .