Skip to main content

Command Palette

Search for a command to run...

What is Node.js? JavaScript on the Server Explained

Updated
10 min read
What is Node.js? JavaScript on the Server Explained

1. What Node.js is

Node.js is a runtime environment that allows you to run JavaScript outside the browser, mainly on the server.

  • Traditionally, JavaScript was only used inside browsers (like Chrome or Firefox).

  • But with Node.js, you can use JavaScript to build backend applications, APIs, and servers.


Simple Definition

Node.js lets developers use JavaScript for both frontend and backend.


Why Node.js was created

Before Node.js:

  • Frontend → JavaScript

  • Backend → Languages like Java, Python, PHP

This meant developers had to learn different languages.

After Node.js:

  • You can use only JavaScript everywhere (frontend + backend)

How Node.js works (basic idea)

  • Node.js is built on Google Chrome V8 Engine

  • It executes JavaScript code very fast

  • It uses a non-blocking, asynchronous model

    • Means it can handle multiple tasks at the same time efficiently

Real-life analogy

Think of Node.js like a waiter in a restaurant:

  • Traditional system → Waiter handles one table at a time

  • Node.js → Waiter takes multiple orders and serves them efficiently without waiting


Example (Simple Node.js code)

// Import built-in module
const http = require('http');

// Create server
const server = http.createServer((req, res) => {
    res.end('Hello from Node.js server!');
});

// Run server
server.listen(3000, () => {
    console.log('Server running on port 3000');
});

👉 When you run this, your computer becomes a server that responds to requests.


Where Node.js is used

  • Backend development (APIs)

  • Real-time apps (chat apps, live notifications)

  • Streaming apps (Netflix-like platforms)

  • Command-line tools


In one line

Node.js is a tool that lets you use JavaScript to build fast and scalable server-side applications.

2. Why JavaScript was originally browser-only

When JavaScript was created, its purpose was very specific — to make web pages interactive inside the browser.


1. It was designed for browsers only

  • JavaScript was introduced in 1995 for Netscape Navigator

  • The goal was simple:

    • Add interactivity to web pages

    • Handle things like button clicks, form validation, and animations

👉 At that time, there was no need to run JavaScript outside the browser


2. No standalone JavaScript engine existed

  • Browsers had built-in engines to run JavaScript

  • For example:

    • Chrome uses V8 JavaScript Engine
  • But outside browsers:

    • There was no environment to execute JavaScript

    • So it couldn’t run on servers or machines directly


3. Security reasons

  • Browsers act as a sandbox (safe environment)

  • JavaScript was restricted to:

    • Web page content

    • Limited access to system resources

👉 This prevented:

  • Access to files on your computer

  • System-level operations

So keeping JavaScript inside the browser made it safe


4. Backend was dominated by other languages

  • Before Node.js:

    • Java → enterprise backend

    • PHP → web backend

    • Python → scripting & backend

👉 JavaScript was seen as a frontend-only language


5. No demand for full-stack JavaScript

  • Early web apps were simple:

    • Static pages

    • Basic forms

👉 So developers didn’t need JavaScript on the server


What changed later?

  • Powerful JS engines like Google Chrome V8 Engine were created

  • Then Node.js used this engine to run JS outside the browser

👉 That’s when JavaScript became full-stack


In one line

JavaScript was originally browser-only because it was designed for web page interactivity, had no runtime outside browsers, and was restricted for security reasons.

3. How Node.js made JavaScript run on servers

Node.js didn’t change JavaScript itself — it provided a way (environment) to run it outside the browser.


(1) . Using a powerful JavaScript engine

  • Node.js uses the same engine as Chrome: Google Chrome V8 Engine

  • This engine can:

    • Compile JavaScript into machine code

    • Run it directly on your computer (server)

👉 So JavaScript no longer depends on a browser to run


(2). Providing a runtime environment

  • Node.js acts as a runtime

  • It gives JavaScript access to:

    • File system (read/write files)

    • Network (handle requests)

    • Operating system

👉 Earlier, JavaScript couldn’t do these things in the browser


(3). Adding built-in modules

Node.js comes with ready-to-use modules like:

  • http → create servers

  • fs → handle files

  • path → manage file paths

👉 These modules make it possible to build backend systems easily


(4). Event-driven, non-blocking architecture

  • Node.js uses:

    • Event loop

    • Asynchronous (non-blocking) execution

👉 Means:

  • It can handle multiple users at once

  • Without waiting for one task to finish


(5). Turning JavaScript into a server language

With Node.js, you can:

  • Create servers

  • Build APIs

  • Handle databases

  • Process requests & responses


Example (Server in Node.js)

const http = require('http');

const server = http.createServer((req, res) => {
    res.end('Running JavaScript on Server!');
});

server.listen(3000);

👉 This code:

  • Creates a server

  • Listens for requests

  • Sends a response


Real-life analogy

Think of JavaScript as a car engine:

  • Before Node.js → Engine was locked inside a car (browser)

  • After Node.js → Engine can be used in trucks, buses, anything (servers)


In one line

Node.js made JavaScript run on servers by using the V8 engine, providing system access, and adding backend capabilities through its runtime and modules.

4. V8 engine overview (high level only)

The V8 engine is the core technology that runs JavaScript code at high speed.

  • It is developed by Google

  • It is used in:

    • Google Chrome

    • Node.js

👉 Basically, whenever you run JavaScript in Chrome or Node.js, V8 is doing the actual work behind the scenes


(1). What V8 actually does

  • Takes JavaScript code

  • Converts it into machine code (understandable by CPU)

  • Executes it very fast

👉 No need for slow interpretation step-by-step


(2). Just-In-Time (JIT) Compilation

  • V8 uses JIT compilation

  • Means:

    • It compiles code while running it

    • Optimizes frequently used code for better performance

👉 Result: Faster execution 🚀


(3). Memory Management (Garbage Collection)

  • V8 automatically handles memory

  • It removes unused objects using garbage collection

👉 So developers don’t need to manually free memory


(4). Why V8 is important for Node.js

  • Node.js uses Google Chrome V8 Engine

  • That’s how Node.js:

    • Runs JavaScript outside the browser

    • Executes code quickly and efficiently


(5). Simple Flow

JavaScript Code → V8 Engine → Machine Code → Execution


Real-life analogy

Think of V8 as a translator + performance booster:

  • JavaScript = English

  • Machine = understands only binary

  • V8 = converts English → machine language instantly and efficiently


In one line

V8 is a high-performance JavaScript engine that converts JS code into fast machine code and powers both Chrome and Node.js.

5. Event-driven architecture idea

Event-driven architecture is a way of designing systems where everything happens in response to events.


(1). What is an “event”?

An event is simply something that happens.

Examples:

  • User clicks a button

  • A request comes to a server

  • A file finishes loading

👉 These actions trigger the system to respond


(2). Core idea

Instead of running code step-by-step in a fixed order:

  • The system waits for events

  • When an event occurs → it executes a specific function (callback/handler)


(3). How it works in Node.js

  • Node.js uses an event loop

  • It continuously listens for events like:

    • Incoming requests

    • Database responses

    • Timers

👉 When an event happens:

  • A corresponding function runs automatically

(4). Simple example

const fs = require('fs');

// Event: File read completed
fs.readFile('file.txt', (err, data) => {
    if (err) throw err;
    console.log('File content:', data.toString());
});

console.log('Reading file...');

👉 Flow:

  • “Reading file…” prints first

  • File read happens in background

  • When done → event triggers callback


(5). Real-life analogy

Think of a food delivery app:

  • You place an order (event)

  • Restaurant prepares food (process)

  • Delivery arrives → notification (event trigger)

👉 System reacts only when something happens


(6). Why it’s powerful

  • Handles multiple tasks efficiently

  • Doesn’t block execution

  • Perfect for:

    • Real-time apps (chat apps)

    • APIs handling many users


(7). Key components

  • Event → Something that happens

  • Event Listener → Waits for the event

  • Callback/Handler → Executes when event occurs


In one line

Event-driven architecture means systems respond to events instead of following a strict sequential flow, making them fast and scalable.

6. Real-world use cases of Node.js

Node.js is widely used in real-world applications because it is fast, scalable, and handles multiple users efficiently.


(1). Real-time Applications (Chat, Live Updates)

https://images.openai.com/static-rsc-4/bLzcq6vE8l-VcnLSI7_6RfsnFqJ5ipo1AijwzIUXsMTEY6IEyZ-b6x4UfP0XXkovlmxDjxnALBNF6HQo8z3gdGovh6A5op6ZUGX3xuLVXJwtg2WNJvMMJeT1oTJzSYi6_yTqaUEAKn9FloJ3cYNiE-9py1VSGKyfoh21cAfiMzGa9Km255450QTHDaPLxalQ?purpose=fullsize
  • Apps where data updates instantly

  • Examples:

    • Chat apps (like WhatsApp)

    • Live notifications

    • Online gaming

👉 Node.js handles multiple users simultaneously using event-driven architecture


2. Streaming Applications

https://images.openai.com/static-rsc-4/QtyKNIXR5RlfFOosCGGbXa7X9cGrDPI14pFPcJ9Fh0q4ocEpvLeca5xC3swqLJrBRYaRm0Zsnw8MQaSJWGZOWYjRd6iw7jcpvhoLekQrPNcKBPB-5R8ZZw8ze-fqtsMcRDS4LXPVhW4UjX-9FsBC0CWfB2boSiTZe6wtm6O3P566AE1zFriCFuOnTRBdNHWZ?purpose=fullsize https://images.openai.com/static-rsc-4/4jdU102wxWjBbv36OWBEaFoUCWTDavHnpo6eQycZwpW9umGvmQATX0FeFbQ-1YpYhZ10uX-uBNwfTee-Etg5SzFNtISs7pBZdp73v3E-LXtdmrLw8JDuFOo7QSp0RbLw_NznReCk36JmP5WzJtAg0WCJH0A0_H3yasA1yQ6syDqLyFYTb0QGt9on_Bc2sj4B?purpose=fullsize

8

  • Used for video & audio streaming

  • Examples:

    • Netflix

    • Spotify

👉 Node.js streams data in chunks, so users don’t need to wait for full data


(3) . REST APIs & Backend Services

https://images.openai.com/static-rsc-4/9tRVy5WFjZ0I_sOHo5TkEhgFBJn_PwTML5V0yx1TOPQbXbDBsrIoNJpNpVr6WRSng8cWol2kHjUavdrhTs0tDgdSDbMtKYDl-S4QhVUsKNbekGyfTatl-fqmbqjliIgqMwOqmnO6qmMBojIuxqfh16ZKRBLAEnkwhKUw_uwWcGDDb6Y82QJxqVC8mQ2tY_MD?purpose=fullsize https://images.openai.com/static-rsc-4/70Cj2BIxNIfuclxGKQktd9rN2LwKqng9g3RBLWQ2oYv9AtjV3_CIfzQxGT0qvMWPZ6-Dzb0Yoy4toUwUXXB5HOSQbUXbwdhaXe6GbUYD0CRq9U9ootlawUHFAhLGj83vEknb_sQxWXVCxzMSri-_plyQO2i7zdHg-Ge6AmKwrpVqqW6fZKaq6dCXZvFlV-ap?purpose=fullsize https://images.openai.com/static-rsc-4/1RPw5qmb9tsl56Rle52beqEpD-K7CXUz_oVMVgNTpZ3VLXko9Pq2Isf9luQz-s-qqvKriQkajVkznqQ0oZc1I2rHT5SobDnNzyPhqkdgUjMoroN1nHk-U_9iCxfMRMhLYp9aOkWWmeT5PXDDafqK5NSU2E1U5WFbyZzeAqukubuUZ9omNiee9g8g91yGaEU9?purpose=fullsize
  • Used to build APIs for:

    • Web apps

    • Mobile apps

👉 Example:

  • Login system

  • Fetching user data

  • Payment processing


(4) . Single Page Applications (SPAs)

https://images.openai.com/static-rsc-4/IDj_-QDNrTMP3ltPfu2R9Kh5tLvoCpK-YWqbSjf4E0u6O9mqnO905BVMjzZEzP1PlGmFMvH8lCX_wgtMZoH0_1ZPQGM1fRFvvU7jhzpQ-dNlxjZ_xPuXd6pPNJ2klw9mb1fw6UFAtWP6AHNpZlpbCCuwDTonusNLp5ZWY5T6Y_VW0rONRaZUIugnqkHgNExC?purpose=fullsize https://images.openai.com/static-rsc-4/ng6RtHNTg2NrJJVm6dpXrPiF_YfbBCNwqqlKohKQGc04AOlO8cMva4QDYRF2ix13SQh8wzyNYclXgQyo1OiUxEoFOA6x7i_47eTENThftaAZcV7KOKFKudn9wBORVpzkCkqKaqi2zHYEEN-wwFL3q_whdRlvEt9KWP9EvnvQDsgwesweHH5uLN_cAeHdY1Z-?purpose=fullsize https://images.openai.com/static-rsc-4/fJxbGfUeX90PadfSPi9RxFcJLUpiXIy5vV9ChgfpWA6DM4JwA_yHtH4Y3GTE3o-LtYwAi3VbSnd0hW8BVPpUVeMWDOyx7nkYn1ScgPlv818FI-GYKHJsx5MljNXh06yJ2K0F3UpHNQle93xyiBhk9MU7EIRZUEeKumB-_tvuMhxxYwGm8emK8FeqivYnQa0N?purpose=fullsize
  • Fast-loading web apps with dynamic content

  • Node.js works with frontend frameworks like:

    • React

    • Angular

👉 Used in dashboards, admin panels, SaaS apps


(5) . Microservices Architecture

https://images.openai.com/static-rsc-4/fK1iVD1dIvi15ho4rlNMDaHsQ4TSoHUnhnfhwt-QpVWgKuUkDff401fwxX2ZcPv8KjxHiU8hVhdErPCmHH20vMm_E-qRrA4mHcqemBPFAjTCmQbQ9Hey48vR5m89BEF5zsfi19xuzlrvKk3UPjai8ocR5X6Es9_VI--zEv_Hdmjol5gs-G3EJ0DcKgHaxtGK?purpose=fullsize https://images.openai.com/static-rsc-4/rDHc1RvRYo0Aw7g_e3fyhL-HKpGYHaXTUJGWumi74D2rtne2JBlP0u4k3AYlR0o-PddxAwlDVV13yXHTle4XVdn_AyaQyVuPna7mGOv5AmNvoy6NBetWpy8ykn351Fcodw8fKXxolD-tx_ZipNvKYNU8l2LTzVowYc4_AR296WOn-0mY5Bb8aW-HLj9WSv2Y?purpose=fullsize https://images.openai.com/static-rsc-4/7BJgaiW3WTv0g1hUyFzHAwR0Eq0YqX3XTKdCNtywVnjluHxkKSBvdlD-_R4L-vlvBDzn5Bnj3XaX0RJw8KgBs4fywDWSoPnQZ8saGTNUy9M8dhTwp73GID1n9S2qo-kc4wzhnFJ3CyqGR4s9nxvBcJRhNRj_gd9LHAo096BYqaNvlEEi9AmaNT8OZ9-d40iv?purpose=fullsize
  • Large apps are divided into small services

  • Each service handles a specific task

👉 Node.js is lightweight, so perfect for building microservices


(6) . Command Line Tools (CLI)

https://images.openai.com/static-rsc-4/HcZUXmIFG7XQOKymGtcZOH8vIkOhfdhBqJO436Ck_qhr_FHDdP58llqruIajaB8PtEmFxbzaMN9wA336Ym7znUOt24jklilyWbWtfs7ixG60oS83PYtAnRsGzcoPkJku2iHNm_7bKPG3QTR0VhJvdQuIrCoO551mAKCFuOSuWq_ma7-ZPJL016-adUNx8LRm?purpose=fullsize https://images.openai.com/static-rsc-4/SmrO1qyL83niDxn1On35YmfDBefcM3cXWOkYYpXy4aDIbn8w4B6SBF5ywNpTe_MvNJqH0HcDAgaE93rTQJwpu5tt1PJkW50LpP6YBNsn1NNlrHy4FqcdceUJtaLZWX1CqjGS6H47vjzrWoR5lO_ZfTrJZRSMnWjhYvDmSzO6JJnywcLxUOUWZ579j2OaapiV?purpose=fullsize https://images.openai.com/static-rsc-4/sRq2yz5poGY9M3P894d11Yafi-982ErEN-RKOYmIWW0yHnQxiAkT8vFfGjXNxx6assje4xBTr1lte1Ys1WETt6WVq7h0DwdAw5rK62gYZHZQw30CRLjrCXn7JSkcpcKi1SpfCk7o9KzOouqEBr4emswYGIwwvJ2SiLesX2ChFRUzMy5L3sjo3Gc9hTXdqVAx?purpose=fullsize
  • Tools that run in terminal

  • Examples:

    • npm (Node Package Manager)

    • Build tools

👉 Developers use these daily for automation


  • Netflix → Streaming backend

  • PayPal → Faster APIs

  • LinkedIn → Scalable backend

👉 These companies use Node.js for performance and scalability


In one line

Node.js is used in real-world for real-time apps, streaming platforms, APIs, scalable systems, and developer tools.