Skip to main content

Command Palette

Search for a command to run...

What is Node.js? JavaScript on the Server Explained

Updated
3 min readView as Markdown

Node.js is a runtime environment that allows JavaScript to run outside the browser.

Before Node.js, JavaScript was mainly used only for frontend browser interactions like:

  • button clicks

  • DOM manipulation

  • form handling

With Node.js, JavaScript can now also run on:

  • servers

  • backend applications

  • command-line tools

  • APIs


Why JavaScript Was Originally Browser-Only

JavaScript was created for browsers to make web pages interactive.

Browsers already had:

  • JavaScript engines

  • APIs like DOM and events

So JavaScript depended on the browser environment to run.

Without a browser:

  • there was no engine to execute JavaScript

  • no system APIs for file handling or networking

That is why JavaScript was originally browser-only.


How Node.js Made JavaScript Run on Servers

Node.js took the Chrome V8 JavaScript engine and combined it with C++ features and system-level APIs.

Main parts of Node.js:

  • V8 Engine → executes JavaScript

  • C++ bindings → connect JS with system operations

  • libuv → handles async operations and event loop

libuv provides:

  • event loop

  • thread pool

  • async file system handling

  • networking support

This allowed JavaScript to:

  • access files

  • create servers

  • handle requests

  • work outside browsers


V8 Engine Overview (High Level)

V8 is Google Chrome’s JavaScript engine.

Its job is:
convert JavaScript into machine code so the computer can execute it quickly

Node.js uses V8 internally to run JavaScript code.

Example:

console.log("running with node");

V8 compiles and executes this code.


Event-Driven Architecture Idea

Node.js follows an event-driven architecture.

This means:

  • actions happen based on events

  • functions run when something occurs

Examples of events:

  • timer completed

  • file loaded

  • request received

  • API response returned

When an event occurs:

  • its callback/task is placed into a queue

  • the event loop later executes it when the call stack becomes empty


Example

setTimeout(() => {
  console.log("timer completed");
}, 1000);

What happens:

  1. Timer starts in background using libuv

  2. Main thread continues execution

  3. Timer completes → event occurs

  4. Callback enters queue

  5. Event loop pushes callback to call stack

  6. Callback executes

So:

  • event = something happened

  • callback/task = code waiting to execute


Role of libuv

libuv is one of the core parts of Node.js.

It provides:

  • event loop

  • async I/O handling

  • thread pool

Without libuv, Node.js would not be able to efficiently handle asynchronous operations.

It helps Node.js remain:

  • non-blocking

  • scalable

  • efficient


Real-World Use Cases of Node.js

Node.js is commonly used for:

APIs and Backend Servers

const http = require("http");

http.createServer((req, res) => {
  res.end("Hello");
}).listen(3000);