Node.js
Last edited by dave on 01/12/2025, 08:11:32 UTC
Contents
Quick Snapshot
- Type: JavaScript runtime built on Chrome’s V8 engine.
- Created by: Ryan Dahl.
- First released: 2009.
- Core idea: Run JavaScript outside the browser — fast, event-driven, and scalable.
- Maintained by: OpenJS Foundation.
- Tagline (unofficial): “Because JavaScript needed a night job.”
- Website: nodejs.org
A Brief History
Back in 2009, Ryan Dahl looked at how servers were handling requests — one thread per connection — and said, “What if we didn’t do that?”
The result was Node.js, a lightweight, non-blocking runtime that used JavaScript for server-side programming.
Its foundation, Google’s V8 engine, made execution blazing fast. Combine that with asynchronous I/O and event loops, and Node.js became a new paradigm for web servers — capable of handling thousands of concurrent connections.
Core Concepts
Event-Driven Architecture
Node.js operates on a single thread using an event loop to handle concurrent requests without blocking.
When a request involves I/O (reading files, fetching data, etc.), Node delegates it to the system, then moves on to other tasks until the result is ready.
This means one process can handle many clients efficiently — perfect for APIs, real-time apps, and chat servers.
Non-Blocking I/O
Instead of waiting for operations to complete, Node.js uses callbacks, promises, or async/await to continue execution.
Example:
const fs = require("fs"); fs.readFile("example.txt", "utf8", (err, data) => { if (err) throw err; console.log(data); });
The file reads in the background while Node.js keeps doing other work.
The JavaScript Runtime
Node.js uses:
- V8 Engine — Compiles JavaScript to native machine code.
- libuv — Provides the event loop, file system, and asynchronous I/O.
- C/C++ bindings — Expose system-level functionality.
Together, they make JavaScript capable of doing serious backend work — not just running in browsers.
Modules & Packages
CommonJS Modules
Node introduced its own module system:
const http = require("http"); module.exports = function() { ... };
ES Modules (Modern)
Now Node supports ES import/export syntax:
import fs from "fs"; export function greet() { console.log("Hello!"); }
npm (Node Package Manager)
Node’s package ecosystem, npm, is the largest in the world — with over a million modules.
You can install packages with:
npm install express
Popular Frameworks & Libraries
- Express.js: The minimalist web framework that made Node famous.
- NestJS: Opinionated, TypeScript-based structure for large apps.
- Fastify: A high-performance alternative to Express.
- Socket.io: Real-time, bidirectional communication.
- Next.js / Remix: Frameworks that leverage Node for server rendering.
Example: A Simple Web Server
const http = require("http"); const server = http.createServer((req, res) => { res.writeHead(200, { "Content-Type": "text/plain" }); res.end("Hello from Node.js!\n"); }); server.listen(3000, () => { console.log("Server running on http://localhost:3000"); });
Asynchronous Patterns
Node’s async personality has evolved:
- Callbacks: The original (and sometimes messy) approach.
- Promises: Cleaner chaining of async results.
- Async/Await: Modern syntax that reads like synchronous code.
Example:
const fetch = require("node-fetch"); async function getData() { const res = await fetch("https://api.example.com/data"); const data = await res.json(); console.log(data); }
File System, Network, and More
Node comes with powerful built-in modules:
fs— File system operations.http/https— Web servers and clients.path— Handle file paths across systems.os— System info (CPU, memory, etc.).events— Create and listen to custom events.child_process— Spawn subprocesses.
It’s like having the command line and a browser runtime fused into one tool.
Package Management & Tooling
- npm: Default package manager.
- Yarn / pnpm: Alternatives with better performance and dependency handling.
- nvm: Node Version Manager, for juggling multiple Node versions.
- ESLint / Prettier: Keep your JS tidy.
- TypeScript: Add static types to your Node projects for sanity and safety.
Use Cases
Node.js shines in scenarios like:
- APIs and microservices
- Real-time apps (chats, dashboards, live updates)
- Command-line tools
- Serverless functions
- Streaming services
- Single-page app backends
Basically, anywhere speed and scalability matter.
Performance Tips
- Use async APIs wherever possible.
- Avoid blocking the event loop (no heavy CPU tasks inline).
- Cache frequently accessed data.
- Use clustering or worker threads for CPU-bound workloads.
- Monitor with tools like PM2 or built-in
perf_hooks.
Fun Facts
- Node.js was inspired by event-driven servers like Nginx.
- “Node” was chosen because it represents a small, distributed piece of a larger network.
- npm originally stood for “Node Package Manager,” but now it’s more of a brand than an acronym.
- Entire frameworks (like Next.js) and apps (like VS Code) are built on Node.js.
Backlinks (2)
- General
- User
No backlinks yet.
- Redirects
No backlinks yet.
- Media
No backlinks yet.
- Categories
No backlinks yet.
Categories (0)
No categories assigned to this page.
Edit Level
> Signed In Users
Latest on Lounge
Join the conversation about the 'Node.js' article →
No comments yet. Be the first to comment!