Node JS Flashcards
How does Node.js handle asynchronous operations?
Node.js is asynchronous and operates on a single thread that doesn’t have to wait for operations to complete before starting others. It uses an event queue to manage pending operations.
What type of projects is Node.js not suitable for?
Node.js is not suitable for high CPU-intensive projects like image processing, due to its single-threaded nature, which can block the event loop. It is better suited for network-intensive projects.
What language and engine is Node.js built on?
Node.js is built on C++ and uses Google’s V8 engine.
How do you run a JavaScript file with Node.js?
You can run a JavaScript file with Node.js by typing node <filename>
in the terminal.
What is the equivalent of global objects in Node.js compared to the browser?
In the browser, global objects like console and setTimeout are prefixed with window (e.g., window.console.log). In Node.js, these objects are prefixed with global (e.g., global.console.log). Variables you define in the global scope are not prefixed.
What is the purpose of the main module in a Node.js application?
Every Node.js application has a main module. You can inspect its properties using console.log(module). This helps prevent variable conflicts because each file is a separate module.
How can you scan for errors in a Node.js application?
You can use jshint in the terminal to scan for errors in a Node.js application.
What does the module wrapper function in Node.js do?
Node.js wraps each module in a function with the signature function (exports, require, module, __filename, __dirname) { … } behind the scenes. This allows for module encapsulation.
Is require a global function in Node.js?
require seems like a global function, but it is not. It is provided to each module by the module wrapper function.
What is exports in Node.js?
exports is an alias for module.exports and is used to export functions and objects from a module.
What are the main phases of the Node.js event loop?
- Timers: Executes callbacks scheduled by setTimeout() or setInterval().
- Pending Callbacks: Executes I/O callbacks deferred to the next loop iteration.
- Idle, Prepare: Used internally by Node.js.
- Poll: Retrieves new I/O events and executes I/O-related callbacks.
- Check: Executes setImmediate() callbacks.
- Close Callbacks: Executes close callbacks, e.g., socket.on(‘close’, …).
What is the purpose of the Timers phase in the Node.js event loop?
The Timers phase executes callbacks scheduled by setTimeout() or setInterval().
What happens during the Pending Callbacks phase?
I/O callbacks that were deferred to the next loop iteration are executed during this phase.
Explain the Poll phase in the Node.js event loop.
The Poll phase is responsible for retrieving new I/O events and executing I/O-related callbacks. It will block if there are no scheduled timers, waiting for I/O events.
When are setImmediate() callbacks executed in the Node.js event loop?
setImmediate() callbacks are executed during the Check phase of the event loop.