REVIEWER
ELECTIVE 3: Chapter 1 – 4
CHAPTER 1: INTRODUCTION TO NODE.JS
Why Use Node.js?
Fast Execution: Built on the V8 engine, optimized for speed.
Scalability: Handles large-scale applications with ease.
Single Programming Language: Full-stack JavaScript (front-end and back-end).
Vast Ecosystem: Rich package ecosystem via npm (Node Package Manager).
Node.js Event-Driven Architecture
Event-Driven Model: Node.js uses an event loop to manage asynchronous operations.
Key Concepts:
Event Emitter: Listens and responds to events.
Callbacks: Functions that are invoked when asynchronous operations complete.
Use Cases: Real-time applications like chat apps, live feeds, or streaming services.
How the Event Loop Works
Incoming request
Non-blocking I/O operations
Event loop handles callbacks when operations complete
Setting Up Node.js
Step 1: Download Node.js from the official website nodejs.org
Step 2: Install Node.js and verify installation via the terminal.
Command: node -v and npm -v
Step 3: Explore the npm package manager.
Basic Development Environment Setup
Code Editor: Install VS Code, Sublime Text, or any preferred editor.
Folder Structure: Organize your project into clear folders (e.g., /src, /routes).
Create a Simple Application:
REVIEWER
ELECTIVE 3: Chapter 1 – 4
Initialize npm: npm init
Create app.js:
• console.log("Hello, Node.js!");
Running a Node.js Application
Command: Run your application in the terminal:
node app.js
Expected Output: Hello, Node.js!
CHAPTER 2: BASIC NODE.JS PROGRAMMING
Subtopic
Node.js core modules (fs, path, http)
Writing basic Node.js applications
Understanding the Node.js runtime environment
fs (File System)
Provides functions to work with the file system, enabling you to interact with files
and directories—such as reading, writing, creating, and deleting files. It’s one of the
core modules, so you don’t need to install anything extra to use it.
Reading Files
You can read the content of a file.
Writing Files
You can create or overwrite files with the writeFile() method.
Appending Data
Instead of overwriting, you can add data to the end of an existing file.
Deleting Files
You can delete files using the unlink() method.
Creating Directories
You can create directories using the mkdir() method.
Reading Directories
You can read directories using the readdir() method.
path
Provides utilities for working with file and directory paths. It helps in handling and
manipulating file paths in a platform-independent way, which means it works the same on
both Windows and Unix-like systems (like macOS and Linux).
REVIEWER
ELECTIVE 3: Chapter 1 – 4
path.join()
This method joins different parts of a file path into one complete path. It makes sure to
use the correct path separator (/ for Unix, \ for Windows).
path.basename()
This method returns the last part of a file path, typically the file name.
path.dirname()
This returns the directory part of a file path (everything except the file name).
path.extname()
This method extracts the file extension from the file name.
Provides the ability to create servers that can respond to client requests using the HTTP protocol.
This protocol is the foundation of data communication on the web, allowing browsers to request
data from servers and receive responses like web pages or other resources.
http.createServer()
The server listens for incoming requests and responds with data. When a client (such as a web
browser) sends an HTTP request, the server processes it and sends back a response.