Introduction to Node.js

Himanshu MauryaHimanshu Maurya
5 min read

Before we dive into the world of Node.js, I hope you're already familiar with the fundamentals of JavaScript.

What is Node.js ?

Node.js is an open-source, cross-platform JavaScript runtime environment that lets you run JavaScript code outside the browser typically on the server.

Traditionally, JavaScript could only run in web browsers, so it was used just for frontend development. But with Node.js, JavaScript can also run on the server (backend), allowing us to build full-stack applications using just one language.

Node.js was created by Ryan Dahl in 2009. It's fast, lightweight, and works well with events and asynchronous tasks, which makes it great for building scalable network applications.

Node.js vs Traditional Backend Languages

Feature/AspectNode.jsPHPJava
Execution ModelNon-blocking, asynchronousBlocking, synchronous by defaultMulti-threaded, blocking
PerformanceVery high due to event loopSlower in concurrent operationsHigh with multithreading
Concurrency ModelSingle-threaded, event-drivenMulti-process per requestMulti-threaded
ScalabilityHorizontal scaling, efficient I/OLimited scalabilityScales well with threads and JVM

Key Benefits over PHP and Java

  • Same Language Everywhere: You can use JavaScript for both frontend and backend.

  • Faster Development: Reuse code easily and work smoothly with JSON APIs.

  • Great for Real-Time Apps: Perfect for apps that use Web Sockets, like chat apps.

  • Fast and Lightweight: Runs quickly thanks to the V8 engine and non-blocking I/O.

  • Lots of Tools: NPM gives access to thousands of ready-to-use packages.

Node.js Setup and Hello World Server

  • Step 1: Install Node.js

  • Step 2: Verify Installation

    Open Command Prompt and run

      node -v
      npm -v
    
  • Step 3: Create Your Hello World Server

    Make a file server.js in new folder (eg. HelloWorld)

      // server.js
    
      const http = require('http');
    
      const hostname = 'localhost';
      const port = 3000;
    
      const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain');
        res.end('Hello, World!\n');
      });
    
      server.listen(port, hostname, () => {
        console.log(`Server running at http://${hostname}:${port}/`);
      });
    
  • Step 4: Run the Server

    • Open terminal or command prompt and go to that particular folder where your server.js file exists

(eg. C:\HelloWorld)

  • Run the below command:
    node server.js

You will see : Server running at http://localhost:3000/

Open your browser and write : http://localhost:3000
You should see: Hello, World!

  • Congratulations, you have written your first "Hello World" in Node.js!

Why Use Node.js?

Handles Many Requests at Once: It doesn't wait for one task to finish before starting another.

Great for Real-Time Apps: Ideal for things like chat apps or online games.

Same Language Everywhere: Use JavaScript for both frontend and backend.

Lots of Ready-Made Tools: Thanks to npm, you get access to over a million helpful packages.

How Node.js Works Internally ?

Node.js uses the V8 engine (also used in Google Chrome), which turns JavaScript into machine code. This makes it super fast.

The real magic is in how it handles tasks:

  • Instead of waiting for slow tasks (like reading a file or talking to a database), Node.js hands them off and keeps going.

  • When the task is finished, it calls back with the result.

  • This way, Node.js can handle many users at the same time without getting stuck.

Core Features :

1 . Asynchronous and Event-Driven

Everything in Node.js, including HTTP requests, file system operations, and database queries, can be asynchronous.

  • This means Node.js doesn’t block the execution while waiting for a task to complete.

      const fs = require('fs');
      fs.readFile('data.txt', 'utf8', (err, data) => {
        if (err) throw err;
        console.log(data);
      });
      console.log("File read requested");
    

    Output -

      File read requested
      (data from file)
    

2. npm – Node Package Manager

  • Comes bundled with Node.js.
  • Offers access to 1M+ reusable packages (like Express, Mongoose, Lodash).

  • Lets you install packages using simple commands:

      npm install express
    

3. Built-in Modules

Node.js provides built-in modules like:

  • http: Create servers

  • fs: Interact with the file system

  • path: Handle file paths

  • os: Get OS-level information

      const os = require('os');
      console.log(os.platform()); // linux, win32, darwin, etc.
    

4. Modular Architecture

Code can be broken into modules (like functions and classes).

  • You can require your own files or third-party libraries to keep code clean and organized.

      // add.js
      module.exports = function (a, b) {
        return a + b;
      };
    
      // index.js
      const add = require('./add');
      console.log(add(2, 3)); // 5
    

Use Cases :

1. Web Applications

Using frameworks like Express.js, you can build scalable web apps and REST APIs.

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

app.get('/', (req, res) => {
  res.send('Welcome to Node.js with Express!');
});

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

2. Real-time Applications

Node.js is ideal for real-time communication (e.g., chat apps, multiplayer games) using Web Sockets with libraries like Socket.IO.

3. Microservices and APIs

Build lightweight, fast, RESTful APIs that scale easily.

4. Command Line Tools

Many popular tools like npm, gulp, and webpack are built using Node.js.

When Node.js is Not the Best Fit :

Not the Best Fit
Node.js isn’t the best choice for heavy tasks like video rendering, complex math, or machine learning. That’s because it runs JavaScript on a single thread, and these tasks can slow everything down. For such jobs, languages like Python, Go, or C++ work better.


Summary
Node.js is a tool that lets you run JavaScript on your computer or server, not just in the browser. This means you can use the same language (JavaScript) to build both the front and back parts of a website or app. It's very fast because it uses Google’s V8 engine, and it can handle lots of users at the same time without slowing down. That makes it great for things like chat apps, games, or websites that need to update in real-time. Node.js also gives you access to thousands of free tools (called packages) to help you build faster and easier. Overall, it’s a powerful and popular choice for making modern web apps.

Read More...


I’m truly thankful for your time and effort in reading this.

10
Subscribe to my newsletter

Read articles from Himanshu Maurya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Himanshu Maurya
Himanshu Maurya

Hi, Thank-you for stopping by and having a look at my profile. Hi! I’m a web developer who loves working with the MERN stack . I enjoy making interactive and user-friendly websites and webapps. I’m great at taking ideas and bringing them to life through coding!