THE NODE ECOSYSTEM
The Node.js ecosystem consists of various libraries, modules, and tools that
enhance development productivity and enable developers to build powerful
applications.
NPM
➔ NPM is the default package manager for Node.js.
➔ It allows developers to discover, install, and manage third-party libraries and
tools needed for their applications.
➔ It makes reusing code very easy.
➔ It is maintained on http://npmjs.org
➔ It is the fastest growing package manager: http://www.modulecounts.com
Node Core modules
Node.js comes with a set of built-in core modules that provide essential functionalities
without the need for additional installations. Some of the core modules include:
-`fs`: File system operations for reading, writing, and manipulating files.
- `http`: Provides HTTP server and client capabilities for building web applications.
- `https`: Similar to `http`, but for secure HTTPS communication.
- `path`: Utilities for working with file paths and directory structures.
- `util`: Utility functions that enhance development productivity.
Express.js
➔ Express is a minimal and flexible Node.js web application framework that provides a
robust set of features for web and mobile applications.
➔ Provides essential features and middleware for routing, handling HTTP requests,
managing cookies, and more.
➔ Express provides a thin layer of fundamental web application features, without
obscuring Node.js features.
Async Programming
➔ Node.js leverages asynchronous, non-blocking I/O operations, making it highly
efficient for handling concurrent connections.
➔ In async programming task initiates an operation and then continues its
execution without waiting for the operation to complete.
➔ The program doesn’t block, and other tasks can continue their execution
simultaneously. When the asynchronous operation finishes, a callback function or
a promise is used to handle the result or trigger further actions.
➔ Node.js uses event loop for this!
Babel
➔ Babel is a powerful transpiler that allows developers to write modern JavaScript
code using the latest ECMAScript features.
➔ transforms modern JavaScript code into backward-compatible versions, ensuring
compatibility with older Node.js versions and browsers.
ECMAScript
➔ Specifications, standards or rules for a scripting language.
➔ JavaScript is implementation of these standards; ECMA Script.
➔ Ensures browser compatibility and a uniformity.
➔ ECMA- European Computer Manufacturer Association.
Nodemon
➔ Nodemon monitors changes to your Node.js application’s files.
➔ Automatically restarts the server when changes are detected.
➔ Eliminates the need to restart the server after every edit.
Debugging
➔ Debugging tools like Visual Studio Code, Node Inspector, or Chrome DevTools to
simplify the debugging process.
➔ Node.js provides built-in debugging capabilities using the ` — inspect` flag.
Event loop
➔ It initialise when application starts, listens and executes all the events and
callbacks of the events.
➔ It works on a single threaded concept but can perform many I/O operations
concurrently with the help of OS.
Event Emitters & Listener
➔ Objects capable of emitting events - event emitters.
➔ Examples: HTTP servers, file system operations, and database connections.
➔ When an event occurs, the corresponding listener’s callback function is executed
asynchronously.
Callback & Non-Blocking I/O
➔ Non-Blocking model is enabled because of concurrency in handling made
possible using callbacks and event loop.
➔ Callbacks are scheduled once the function has completed execution so the other
functions can be completed without blocking.
Modules
➔ Code is encapsulated in modules to make it reusable.
➔ Node.js has many popular inbuilt modules.
➔ We can build our custom modules also.
Example: creating a custom module
circle.js
const { PI } = Math; const circle = require('./circle.js');
exports.area = (r) => PI * r ** 2; console.log(`The area of a circle of
radius 4 is ${circle.area(4)}`);
exports.circumference = (r) => 2 * PI * r;
V8 Engine
➔ Developed by Google and also used in the Chrome browser.
➔ Highly performant due to compilation to machine code.
Libuv
➔ A cross-platform, open-source library written using C language.
➔ It is responsible for the asynchronous nature of Node.js.
➔ Primarily developed for node.js but is now used by Luvit, Julia, uvloop etc.
Buffer & Stream
➔ Node.js provides the Buffer and Streams API.
➔ allows efficient handling of binary data and large amounts of data.
➔ Streams- process data in small chunks, reducing memory overhead and
improving performance.
Clusters
➔ Cluster module -allows applications to take advantage of multi-core processors.
➔ By creating child processes each running on a separate core which distributes the
workload across multiple cores
➔ improves application performance and scalability.