javascript-today

What is NodeJS

NodeJS is a runtime environment that allows you to run JavaScript on the server-side, outside of a web browser. It was created in 2009 by Ryan Dahl and is built on Chrome’s V8 JavaScript engine.

Why NodeJS?

Before NodeJS, JavaScript could only run in web browsers. NodeJS changed that by providing:

  • Server-side JavaScript: Write backend code in the same language as your frontend
  • Fast execution: Built on Chrome’s V8 engine (compiled to machine code)
  • Non-blocking I/O: Handle thousands of concurrent connections efficiently
  • NPM ecosystem: Access to over 2 million packages

What Can You Build?

NodeJS is great for:

  • Web servers and APIs: RESTful services, GraphQL servers
  • Real-time applications: Chat apps, live notifications
  • Command-line tools: Build scripts, deployment tools
  • Microservices: Scalable backend architectures
  • Full-stack applications: Use JavaScript everywhere

How It Works

// Browser JavaScript
console.log("Hello from browser!");

// NodeJS JavaScript (same syntax!)
console.log("Hello from Node!");

The JavaScript syntax is identical, but NodeJS provides different APIs:

  • No window or document (those are browser-only)
  • Has require() for loading modules
  • Has fs for file system access
  • Has http for creating servers

Event-Driven Architecture

NodeJS uses an event-driven, non-blocking model:

// This doesn't block - code continues while file is read
fs.readFile('data.txt', (err, data) => {
  console.log('File read complete!');
});

console.log('This runs immediately, before file is read');

This makes NodeJS excellent for I/O-heavy applications where you’re waiting for databases, file systems, or network requests.

Browser vs NodeJS

Feature Browser NodeJS
DOM ✅ Yes ❌ No
File system ❌ Limited ✅ Full access
Modules ES6 import CommonJS require
Global object window global
Use case Frontend UI Backend servers

What’s Next?

In the next tutorials, you’ll learn:

  1. How to install NodeJS and run your first script
  2. Working with modules and npm packages
  3. Reading and writing files
  4. Creating HTTP servers
  5. Building web applications with Express