Node.js - A Powerful JavaScript Runtime for Web Applications

Understanding Node.js, Its Features, and How to Deploy Applications

Node.js is a widely used JavaScript runtime that enables developers to build fast, scalable, and efficient server-side applications. Unlike traditional application servers like Apache Tomcat or uWSGI, Node.js operates on a non-blocking, event-driven architecture, making it well-suited for real-time applications and microservices.

Since its release in 2009, Node.js has gained immense popularity among backend developers, powering applications ranging from simple APIs to high-performance enterprise systems. It uses Google’s V8 JavaScript engine, ensuring fast execution of JavaScript code.

In this article, we will explore what Node.js is, its key features, how to install and configure it, and how to deploy a Node.js application.

What is Node.js?

Node.js is an open-source, cross-platform JavaScript runtime that runs on the server side. Unlike traditional web servers that handle requests in a synchronous manner, Node.js uses an event-driven, non-blocking I/O model, allowing it to handle thousands of concurrent connections efficiently.

Key Features:

  • Single-threaded, Event-Driven Architecture – Handles multiple requests asynchronously.
  • Non-blocking I/O – Ideal for real-time applications like chat apps and streaming services.
  • Cross-Platform – Runs on Linux, Windows, macOS, and supports containerization (Docker, Kubernetes).
  • Fast Execution – Uses Google’s V8 engine to execute JavaScript efficiently.
  • Built-in Package Manager (NPM) – Provides access to thousands of libraries and modules.
  • Microservices & API-Ready – Easily build RESTful APIs and microservices.
  • WebSocket & Streaming Support – Ideal for real-time applications.

Node.js is commonly used to build APIs, web servers, real-time applications, and microservices architectures.

Installing Node.js

1. Installing Node.js on Linux

To install Node.js, use the package manager for your distribution.

Install Node.js via APT (Debian/Ubuntu):

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

Install Node.js via DNF (CentOS/AlmaLinux):

dnf module enable nodejs:18
dnf install -y nodejs

Verify the installation:

node -v
npm -v

2. Install Node.js Using NVM (Node Version Manager)

NVM allows you to install and manage multiple Node.js versions.

curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
source ~/.bashrc
nvm install 18

Check the installed version:

node -v

Creating a Simple Node.js Application

A basic Node.js web server can be created using the built-in HTTP module.

1. Create a Simple Web Server

Create a new file server.js:

const http = require('http');

const server = http.createServer((req, res) => {
    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, Node.js Server!\n');
});

server.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});

Run the server:

node server.js

Access the server in a browser or via curl:

curl http://localhost:3000

Using Express.js for Web Applications

While the built-in HTTP module works, most applications use Express.js, a minimalist Node.js web framework.

1. Install Express.js:

npm install express

2. Create an Express.js Server

Create app.js:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Hello from Express.js!');
});

app.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});

Start the server:

node app.js

Running Node.js as a Systemd Service

To run Node.js applications as a service, create a systemd unit file.

sudo nano /etc/systemd/system/nodeapp.service

Add the following content:

[Unit]
Description=Node.js Application Server
After=network.target

[Service]
ExecStart=/usr/bin/node /opt/nodeapp/app.js
Restart=always
User=node
Group=node
Environment=NODE_ENV=production
WorkingDirectory=/opt/nodeapp

[Install]
WantedBy=multi-user.target

Enable and start the service:

systemctl daemon-reload
systemctl enable nodeapp
systemctl start nodeapp

Check the service status:

systemctl status nodeapp

Deploying Node.js with Nginx Reverse Proxy

Node.js runs as a standalone server but is often paired with Nginx as a reverse proxy for better performance, security, and load balancing.

1. Install Nginx:

apt install nginx -y

2. Configure Nginx:

Edit the configuration file:

nano /etc/nginx/sites-available/nodeapp

Add the following content:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Enable the configuration:

ln -s /etc/nginx/sites-available/nodeapp /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx

Now, the Node.js application will be accessible at http://example.com.

Performance Tuning & Optimization

  1. Use PM2 for Process Management: Install PM2:

    npm install -g pm2
    

    Start the application:

    pm2 start app.js --name "nodeapp"
    pm2 save
    pm2 startup systemd
    

    This ensures the application restarts automatically on failures.

  2. Enable Gzip Compression: Use the compression middleware in Express:

    const compression = require('compression');
    app.use(compression());
    
  3. Increase Worker Threads: Use the cluster module to take advantage of multi-core CPUs:

    const cluster = require('cluster');
    const os = require('os');
    
    if (cluster.isMaster) {
        for (let i = 0; i < os.cpus().length; i++) {
            cluster.fork();
        }
    } else {
        require('./app'); // Run the Express server
    }
    

Summary

Node.js is a lightweight, scalable, and powerful application server for modern web applications. Its non-blocking architecture and extensive ecosystem make it a top choice for building APIs, microservices, and real-time applications.

By following this guide, you can install, configure, and deploy Node.js applications, ensuring a stable, high-performance backend for your projects.


See also