node1
node1
js
Behind every scalable application is a strong backend. Let's build one with Node.js!
What is Node.js
1⃣ DOM (Document Object Model) – No document, window, or alert since there's no browser.
2⃣ Web APIs – No fetch(), localStorage, sessionStorage, navigator, etc.
3⃣ Rendering Engine – No support for CSS, HTML, or Canvas.
4⃣ Security Restrictions – No Same-Origin Policy (CORS) since it's a backend environment.
5⃣ Event Loop Differences – Uses its own libuv-based event loop instead of the browser's.
Instead, Node.js provides server-side modules like fs (file system), http (server handling),
and os (system info) to interact with the machine directly. 🚀
JavaScript on the Client (Browser Side)
On the client side, JavaScript runs in the browser and is mainly used for:
JavaScript on the Client (Browser Side)
Displays Web Page: Turns HTML code into what you see on screen.
Making API Calls – Fetching data from servers using fetch() or XMLHttpRequest.
Client-Side Validation – Checking user input before sending it to the server.
Animations & Effects – Creating smooth UI animations with libraries like GSAP or CSS
transitions.
JavaScript on the Server
When JavaScript runs on the server-side using Node.js, it is used for:
Handling HTTP Requests & Responses – Creating web servers with http or Express.js.
✅ File System Operations – Reading, writing, and managing files with the fs module.
✅ Real-Time Communication – Using WebSockets (like socket.io) for chat apps or live updates.
✅ Running Background Tasks – Processing queues, sending emails, or handling scheduled jobs.
1. Authentication: Verifies user identities to control access to the system, ensuring
that users are who they claim to be.
10. Logging and Monitoring: Keeps records of system activity to diagnose issues
and monitor system health and security.
In short, JavaScript in the browser focuses on user interactions, UI updates, and
frontend logic, while Node.js handles backend operations like databases, servers,
and APIs. 🚀
Setting Up Node.js on Windows
Is fs Built-in or External?
Modules help organize code into separate files instead of writing everything in one
large file.
Types of Modules in Node.js
👉 There are three types of modules in Node.js:
1⃣ Built-in Modules (Already available in Node.js)
● Examples: fs, http, path, os
● No need to install separately, just use require("moduleName").
2⃣ Third-party Modules (Installed using npm)
● Examples: express, mongoose, lodash
● Need to install using npm install moduleName.
3⃣ Custom Modules (Your own js files)
● Example: A separate file like math.js that exports functions.
● You can import it using require("./math.js").
What is require in Node.js?
It helps you:
✅ Load built-in Node.js modules (e.g., fs, http)
Parameters:
✅ require("fs") – Loads the File System (fs) module, which allows Node.js to
handle files.
✅ const fs – Stores the imported module in a variable named fs, so we can use
its methods.
Use fs.writeFile()
✅ "writing file" – The text that will be written inside the file.
✅ (err) => { ... } – A callback function that executes after the file operation.
path.join
path.join(__dirname, 'crud_fs');
○ macOS/Linux: /Users/yourname/project/crud_fs
○ Windows: C:\Users\yourname\project\crud_fs
What is REPL in Node.js?
It is an interactive environment where you can run JavaScript code directly in the
terminal without creating a file.
How to Open REPL in Node.js?
2⃣ Type: node
3⃣ Press Enter
4⃣ Now, you can type JavaScript commands and see results immediately!
REPL Features
✅ Mathematical Calculations
In Node.js, the path module provides utilities for working with file and directory
paths. It's a built-in module, so you don't need to install any external packages to
use it.
💡 Why is it needed?
Different operating systems use different path formats:
● Windows: C:\Users\John\file.txt
● Linux/macOS: /home/john/file.txt
The path module helps handle paths correctly, no matter which OS the server runs on.
__dirname & __filename – Get Current File/Folder Path
path.dirname() is useful when you want to extract the directory path from a file or
folder. If you're working with a file path, __dirname is enough, but if you need a
parent directory, use path.dirname(__dirname).
path.parse()
The path.parse() method in Node.js breaks down a file path into different parts:
The path.extname() method extracts the file extension from a given file path.
path.basename()
The path.basename() method extracts the file name (including extension) from a
given file path.
Introduction to HTTP Module
📌 The http module in Node.js allows us to create a web server that can handle
HTTP requests and responses. It provides functionalities to handle:
● Creates an HTTP server and takes a callback function with req (request) and
res (response) objects.
● req contains information about the client's request.
● res is used to send data back to the client.
Understanding writeHead(), write(), and end() in Node.js HTTP Module
Purpose: Sets the HTTP status code and response headers before sending data.
Parameters:
● statusCode → HTTP response code (e.g., 200 for success, 404 for Not Found).
● headers (optional) → An object containing key-value pairs of response headers.
res.writeHead(200, { "Content-Type": "text/plain" });
The server.listen() method is used to start an HTTP server and make it listen for
incoming requests.
server.listen(port, callback);
port (Required)
callback (Optional)