Core NodeJS
Core NodeJS
js Concepts
Node.js is a runtime environment that allows JavaScript to run on the server side. It
uses the V8 JavaScript engine and operates on a single-threaded, event-driven
architecture, making it lightweight and efficient for I/O-heavy tasks.
The Event Loop is a mechanism that allows Node.js to perform non-blocking I/O
operations. It continuously checks the call stack and processes events from the event
queue, enabling asynchronous behavior.
libuv is a C library that provides the Event Loop and handles asynchronous I/O
operations like file system tasks, networking, and timers.
process.nextTick() executes immediately after the current operation, before the Event
Loop continues.
setImmediate() executes in the next iteration of the Event Loop.
Node.js uses the worker_threads module to create child threads for CPU-intensive
tasks, keeping the main thread free for I/O operations.
The Buffer class is used to handle binary data directly, which is essential for working
with files, network protocols, and other I/O operations.
7. What are streams in Node.js?
Streams are collections of data that allow processing data in chunks instead of loading
it all into memory at once. They improve performance and efficiency.
The global object is similar to the window object in browsers. It contains global
variables and functions available throughout the application.
Advanced Node.js Concepts
Clustering allows you to create multiple instances of your Node.js application (workers)
to leverage multi-core systems, improving performance by distributing the load across
CPUs.
The cluster module enables the creation of child processes (workers) that share the
same server port, allowing Node.js to handle more requests efficiently.
Use tools like node-inspect or Chrome DevTools to identify memory leaks. Common
fixes include clearing timers, closing database connections, and avoiding global
variables.
The EventEmitter class is used to create and handle custom events in Node.js. It is the
foundation of the event-driven architecture.
5. What is the difference between spawn(), exec(), and fork() in the child_process
module?
The util module provides utility functions like promisify (to convert callback-based
functions to promises) and inherits (for inheritance).
7. How do you debug a Node.js application?
Use console.log, node-inspect, or debugging tools in IDEs like VS Code. For advanced
debugging, use Chrome DevTools or ndb.
The fs module provides APIs to interact with the file system, such as reading, writing,
and deleting files.
9. What is the difference between readFile and createReadStream in the fs module?
The path module provides utilities to work with file and directory paths, such as joining
paths, resolving relative paths, and extracting file extensions.
Express.js and Middleware
Express.js is a web application framework for Node.js that simplifies building APIs and
web applications by providing routing, middleware, and other utilities.
Middleware are functions that have access to the request (req), response (res), and the
next middleware in the cycle. They are used for tasks like logging, authentication, and
error handling.
Use error-handling middleware with four arguments: (err, req, res, next). This
middleware catches errors and sends appropriate responses.
app.use() is used to mount middleware functions that are executed for every request to
the application.
Use HTTPS, validate user input, sanitize data, implement authentication (e.g., JWT),
and regularly update dependencies to patch vulnerabilities.
Use clustering, caching (e.g., Redis), load balancing, and efficient algorithms. Also,
minimize blocking operations and optimize database queries.
PM2 is used to manage and monitor Node.js applications. It provides features like
auto-restart, load balancing, and logging.