NodeJS
What is NodeJS?
NodeJS is a JavaScript runtime environment based on the V8 engine, and it's typically used as a backend programming language. It was launched in 2009, initially for Linux, but quickly gained popularity and became available for the other OSs.
Node.js has revolutionized the way we build web applications by introducing an asynchronous, event-driven architecture and a powerful single-threaded non-blocking I/O model. This combination enables Node.js to handle multiple concurrent requests efficiently, making it ideal for real-time applications and large-scale data processing.
Event-Driven Architecture
Node.js applications are built upon an event-driven architecture, where the program waits for events to occur rather than continuously polling for data (as opposed to Apache HTTP Server, Nginx, or IIS). This approach allows Node.js to handle multiple concurrent requests without blocking the main thread, making it highly scalable and efficient.
Single-Threaded Non-Blocking I/O Model
Node.js utilizes a single-threaded non-blocking I/O model, meaning that the main thread handles all JavaScript execution. When performing I/O operations, such as reading or writing files, Node.js delegates these tasks to the operating system and continues executing other code. Once the I/O operation completes, an event is emitted, notifying the program that the data is available.
Built-in Modules
Node.js comes with a rich collection of built-in modules that provide essential functionalities for developing web applications. These modules include:
File System (FS): Provides functions for interacting with the file system, such as reading, writing, and deleting files.
HTTP: Provides tools for creating and handling HTTP requests and responses.
Net: Enables network communication, including TCP and UDP sockets.
Crypto: Offers cryptographic functions for secure data handling.
Path: Provides tools for manipulating file and directory paths.
Installation
Use
Open a Command Prompt terminal, directly from OS or through an IDE such as VScode.
Type node -v
to see the current version of NodeJS installed on the system.
For running an app, browse to the app's directory (or copy the path to the app's directory) and type node app.js
(assuming your app's filename is "app").
To interrupt a running app, type CTRL+C
.
Basic Example of starting a server
Popular Node.js tools and libraries
npm (Node Package Manager)
npm is the default package manager for Node.js (launched in 2010). It is used to install, publish, and manage Node.js packages, which are reusable pieces of code that extend the functionality of Node.js applications. npm is an essential tool for Node.js developers, as it allows them to easily find and use the latest and greatest Node.js libraries.
Express.js
Express.js is a popular web application framework for Node.js. It provides a simple and expressive API for routing, middleware, and templating, making it easy to build web applications of all sizes. Express.js is one of the most widely used Node.js frameworks, and it is a great choice for developers who are new to Node.js or who want a simple and flexible framework.
Mocha
Mocha is a JavaScript test framework that is commonly used with Node.js applications. It provides a simple and expressive API for writing unit tests, integration tests, and behavior-driven tests. Mocha is a great choice for developers who want to write comprehensive tests for their Node.js applications.
Chai
Chai is an assertion library for Node.js. It provides a variety of assertions for testing different aspects of your code, such as values, types, and exceptions. Chai is a popular choice for developers who use Mocha, as it provides a clean and expressive API for writing assertions.
Others
In addition to these essential tools, there are many other popular Node.js libraries and frameworks, such as:
Socket.io: Real-time communication library
Passport.js: Authentication middleware
Babel: JavaScript compiler
Webpack: Module bundler
PM2: Process management tool
Basic Express.js Example
To install Express.js, simply run the following command (Node.js must be already installed) in the terminal with the path of your application:
npm install express --save
The --save or --s ensures that the npm library, along with its version and meta-information is added to the package-lock.json
Running the app:
[optional] Writing unit tests
Creating a simple Express application with a single endpoint that returns the sum of two query parameters, and then writing a unit test for this endpoint using Mocha, involves several steps. Here's a breakdown of how you can do this:
Step 1: Project Setup
Initialize a new Node.js project:
Install Express and Mocha (along with chai
for assertions):
Create the files:
app.js
for the Express application.test.js
for the Mocha tests.
Step 2: Create the Express Application (app.js
)
app.js
)Step 3: Create the Unit Test (test.js
)
test.js
)Step 4: Update package.json
for Testing
package.json
for TestingModify the test
script in your package.json
to run Mocha:
Step 5: Run the Tests
This will run your Mocha tests, which will test the /sum
endpoint with valid and invalid inputs.
If successful, you should receive the following output:
Last updated
Was this helpful?