Node.js Tutorial

Subject: Node.js

What is Node.js?

Node.js is an open-source, cross-platform runtime environment that lets you run JavaScript on the server. It's built on Google Chrome's V8 engine, making it fast and efficient for building scalable web applications, APIs, and real-time tools.


Why Use Node.js?

Runs JavaScript on the Server

Node.js enables developers to write both frontend and backend code in JavaScript.

High Performance

Uses the V8 engine to compile JavaScript to machine code for fast execution.

Asynchronous and Event-Driven

Node.js uses non-blocking I/O and an event loop to handle concurrent requests.

Single-Threaded but Scalable

Although it runs on a single thread, the event loop allows it to scale for large applications.

Large npm Ecosystem

The Node Package Manager (npm) gives access to thousands of open-source libraries and tools.


Installing Node.js

  1. Go to https://nodejs.org
  2. Download and install the LTS version.
  3. Verify installation:

Output:


Writing Your First Node.js Program

hello.js

Run Command:

Output:


Creating a Basic HTTP Server

server.js

Run Command:

Terminal Output:

Browser Output:


Initializing a Node.js Project

Creates a package.json file for tracking dependencies and project metadata.


Installing External Modules

Installs Express.js and updates package.json and node_modules/.


Key Takeaways

  • Node.js runs JavaScript on the server-side.
  • Built on Chrome's V8 engine for performance.
  • Uses non-blocking, event-driven architecture.
  • Ideal for building web servers, APIs, and real-time apps.
  • Has built-in modules and a huge package ecosystem via npm.
Next : Node HOME