02 - Introduction to Node.js


Introduction

Node.js is a runtime environment that allows JavaScript to run outside the browser. It is built on Google’s V8 JavaScript Engine and is commonly used for backend development.



1. What is Node.js?

  • Node.js is asynchronous, event-driven, and non-blocking.
  • It uses the V8 engine, which converts JavaScript into machine code for fast execution.
  • It enables server-side scripting, allowing JavaScript to run on a server.

Example:

console.log("Hello, Node.js!");

Run this file with:

node filename.js


2. Installing Node.js


Step 1: Download & Install

node -v
npm -v

Step 2: Using Node REPL

The Node.js Read-Eval-Print-Loop (REPL) allows executing JavaScript interactively:

node
> console.log("Hello");

Exit REPL by pressing Ctrl + C twice.



3. Understanding the Node.js Architecture


Key Features:

  • Single-threaded, non-blocking
  • Uses event-driven architecture
  • Supports asynchronous programming

The Event Loop

The event loop enables asynchronous execution in Node.js.

console.log("Start");
setTimeout(() => console.log("Delayed"), 2000);
console.log("End");

Output:

Start
End
Delayed


4. Working with Core Modules

Node.js provides built-in modules like:

  • fs (File System) – Work with files.
  • http – Create servers.
  • path – Handle file paths.
  • os – Get OS-related information.
  • events – Work with event-driven programming.

Example: Using fs module

const fs = require("fs");
fs.writeFileSync("test.txt", "Hello, Node.js!");
console.log("File written!");


5. Creating a Basic Web Server

Node.js can create HTTP servers without external frameworks.

const http = require("http");
const server = http.createServer((req, res) => {
    res.writeHead(200, { "Content-Type": "text/plain" });
    res.end("Hello, World!");
});
server.listen(3000, () => console.log("Server running on port 3000"));

Run with:

node server.js

Visit http://localhost:3000 in your browser.



6. Using External Packages with NPM

  • NPM (Node Package Manager) allows installing third-party modules.
  • Example: Install Express.js framework
npm install express
  • Check installed packages:
npm list --depth=0


7. Working with Asynchronous Code


Callbacks:

const fs = require("fs");
fs.readFile("test.txt", "utf8", (err, data) => {
    if (err) throw err;
    console.log(data);
});

Promises & Async/Await:

const fsPromises = require("fs/promises");
async function readFile() {
    const data = await fsPromises.readFile("test.txt", "utf8");
    console.log(data);
}
readFile();


🏆 Exercises

  • 1. Verify your Node.js and NPM installation.
  • 2. Create and run a simple script that prints your name in Node.js.
  • 3. Create a file using the fs module and write some text to it.
  • 4. Build a simple HTTP server that returns JSON data.
  • 5. Install a package using NPM and use it in your script.


Conclusion

Node.js is a powerful runtime for executing JavaScript outside the browser. Understanding its core modules, event loop, and async features is essential before moving to frameworks like Express.js.


NoFuture - A new way to learn it stuff

Sn0wAlice

NoFuture Menthor - Cybersec Analyst

I'm Alice Snow, a cybersecurity professional with a passion for Blue Team operations, defensive security, and compliance. I focus on creating practical solutions to help organizations strengthen their security posture. I’m also involved in offensive CI/CD research and incident detection, always looking for ways to bridge the gap between security theory and real-world application.

Profile Profile