A comprehensive guide to learning Node.js and Express.js fundamentals with hands-on examples.
- Getting Started
- Chapter 1: File Operations
- Chapter 2: Express.js Basics
- Chapter 3: Sessions & Authentication
- Chapter 4: Advanced Topics
npm init -yThis creates a package.json file with default settings.
npm install express express-session cookie-parserAdd this to your package.json:
"type": "module"Learn CRUD operations (Create, Read, Update, Delete) with the Node.js fs module.
Create a File
import fs from 'fs';
fs.writeFile("parna.txt", "Hello parna", (err) => {
if(err) throw err;
console.log("File created");
});Read a File
fs.readFile("parna.txt", 'utf-8', (err, data) => {
if(!err) {
console.log(data);
} else {
console.log(err);
}
});Update a File
fs.appendFile("parna.txt", "\n How are you?", (err) => {
if(err) throw err;
console.log("File updated");
});Delete a File
fs.unlink("parna.txt", (err) => {
if(err) throw err;
console.log("File deleted");
});- Asynchronous file operations
- Error handling with callbacks
- CRUD operations on files
Build your first web server with Express.js!
Basic Server Setup
import express from 'express';
const app = express();
app.get('/', (req, res) => {
res.send("Welcome to my server");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});Route Parameters
app.get('/:name', (req, res) => {
const {name} = req.params;
res.send(`Welcome to ${name} page`);
});Query Parameters - File Creation
app.get("/sign", (req, res) => {
const {file, content} = req.query;
fs.writeFile(file, content, (err) => {
if (err) throw err;
console.log("file created");
});
res.send("File is created");
});Query Parameters - File Rename
app.get("/rename", (req, res) => {
const {oldFile, newFile} = req.query;
fs.rename(oldFile, newFile, (err) => {
if (err) throw err;
console.log("file renamed");
});
res.send("File renamed successfully");
});- Setting up Express server
- Route handling (GET requests)
- Route parameters (
req.params) - Query parameters (
req.query) - Integrating file operations with Express
Learn how to handle user registration, login, and sessions.
Setup Middleware
import session from 'express-session';
import express from 'express';
const app = express();
app.use(express.json());
app.use(session({
secret: 'your-secret-key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));User Registration
const db = [];
let id = 0;
app.post('/register', (req, res) => {
try {
const {name, email, password} = req.body;
const user = {id: ++id, name, email, password};
db.push(user);
res.status(201).send({message: "User registered successfully"});
} catch (error) {
res.status(500).send({message: "Internal Server Error"});
}
});User Login with Cookie
app.post("/login", (req, res) => {
try {
const {email, password} = req.body;
const user = db.find(x => x.email === email && x.password === password);
if (!user) {
res.status(401).send({message: "Invalid email or password"});
}
const token = user.id + "-" + new Date().getTime();
res.cookie("token", token, {httpOnly: true});
res.send({message: "Login successful"});
} catch (error) {
res.status(500).send({message: "Internal Server Error"});
}
});- Express middleware (
express.json()) - Sessions management
- Cookies (httpOnly)
- POST requests
- Request body parsing
- Basic authentication flow
- Error handling with try-catch
- HTTP status codes (201, 401, 500)
import zlib from "zlib";
import { promisify } from "util";
const gzip = promisify(zlib.gzip);
const gunzip = promisify(zlib.gunzip);- Data compression
- Promisify for async/await
- Working with Node.js built-in modules
node file-ops.jsnode app.jsThen visit: http://localhost:3000
node cookies.jsTest with tools like Postman or curl:
# Register
curl -X POST http://localhost:3000/register \
-H "Content-Type: application/json" \
-d '{"name":"John","email":"john@example.com","password":"password123"}'
# Login
curl -X POST http://localhost:3000/login \
-H "Content-Type: application/json" \
-d '{"email":"john@example.com","password":"password123"}'After completing this tutorial, students will understand:
- β Node.js file system operations (CRUD)
- β Setting up Express.js server
- β Routing and request handling
- β Middleware usage
- β Session management
- β Cookie-based authentication
- β HTTP methods (GET, POST)
- β Error handling
- β Working with request parameters and body
teaching-basic-nodejs/
βββ app.js # Express basics & file operations
βββ cookies.js # Authentication & sessions
βββ file-ops.js # File CRUD operations
βββ zip.js # Compression utilities
βββ package.json # Project dependencies
βββ diagram.png # Visual reference
βββ README.md # This file
Happy Learning! π
