Pages

Saturday, April 11, 2026

netwok lab

πŸš€ LAB SERIES: Web Architecture + Performance + Node.js

We’ll do 20 LABS total
πŸ‘‰ Starting with LAB 1–5 (Foundation + Tools)


πŸ§ͺ πŸ”₯ LAB 1: Inspect HTTP Request (Chrome DevTools)

🎯 Goal

Understand how browser → server communication works

πŸ› ️ Steps

  1. Open Chrome
  2. Go to any website (example: google.com)
  3. Press F12 → Network tab
  4. Refresh page

πŸ‘€ What You’ll See

  • Request URL
  • Method (GET)
  • Status (200 OK)
  • Response size

πŸ“Œ From your PDF:
πŸ‘‰ Browser sends HTTP request → server responds


πŸ’‘ Output Understanding

GET / → Request
200 OK → Response

πŸ§ͺ πŸ”₯ LAB 2: View Response Time (Performance Insight)

🎯 Goal

Understand why speed matters

πŸ› ️ Steps

  1. Open DevTools → Network
  2. Click any request
  3. Go to Timing tab

πŸ“Š Observe:

  • DNS lookup time
  • Connection time
  • Response time

πŸ“Œ From PDF:
πŸ‘‰ Even 100ms delay impacts revenue


πŸ§ͺ πŸ”₯ LAB 3: Enable/Disable Cache

🎯 Goal

Understand caching impact

πŸ› ️ Steps

  1. Open DevTools → Network
  2. Tick Disable Cache
  3. Refresh page

πŸ‘€ Compare:

ModeSpeed
Cache ONFast
Cache OFFSlow

πŸ“Œ From PDF:
πŸ‘‰ Cache reduces repeated requests


πŸ§ͺ πŸ”₯ LAB 4: GZIP Compression Check

🎯 Goal

See real compression

πŸ› ️ Steps

  1. Open DevTools → Network
  2. Click request
  3. Check Headers → Content-Encoding

✅ Output

Content-Encoding: gzip

πŸ“Œ From PDF:
πŸ‘‰ 100KB → 10KB after compression


πŸ§ͺ πŸ”₯ LAB 5: Build Your First Node Server

🎯 Goal

Create backend server


πŸ› ️ Step 1: Install Node.js

πŸ‘‰ https://nodejs.org
Check:

node -v

πŸ› ️ Step 2: Create Project

mkdir lab5-server
cd lab5-server
code .

πŸ› ️ Step 3: Create server.js

const http = require('http');

const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello from Node Server πŸš€');
});

server.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});

πŸ› ️ Step 4: Run

node server.js

πŸ‘‰ Open browser:

http://localhost:3000

✅ Output

Hello from Node Server πŸš€

πŸ“Œ Matches your Node.js PDF server example


🧠 REAL UNDERSTANDING

What just happened?

Browser → Request → Node Server → Response → Browser

πŸ‘‰ You built:
✔ Presentation (browser)
✔ Logic (Node server)
✔ (Data layer coming next)


🎯 MINI CHALLENGE πŸ”₯

Modify code:

if (req.url === '/about') {
res.end('About Page');
} else {
res.end('Home Page');
}

πŸ‘‰ Test:

  • /
  • /about

πŸš€ LAB 6–10 (REAL BACKEND + REST + MINI PROJECT)


πŸ§ͺ πŸ”₯ LAB 6: Serve HTML Page (Frontend + Backend)

🎯 Goal

Connect Node server → HTML page


πŸ› ️ Step 1: Create index.html

<!DOCTYPE html>
<html>
<head>
<title>My App</title>
</head>
<body>
<h1>Welcome to My Web App πŸš€</h1>
</body>
</html>

πŸ› ️ Step 2: Update server.js

const http = require('http');
const fs = require('fs');

const server = http.createServer((req, res) => {
fs.readFile('index.html', (err, data) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(data);
res.end();
});
});

server.listen(3000);

▶️ Run

node server.js

πŸ‘‰ Open: http://localhost:3000


✅ Output

πŸ‘‰ Your HTML page loads

πŸ“Œ Same concept as your PDF:
πŸ‘‰ Server reads file → sends response


πŸ§ͺ πŸ”₯ LAB 7: Create JSON API (REST API)

🎯 Goal

Build API like real apps (Swiggy, Amazon)


πŸ› ️ Code

const http = require('http');

const server = http.createServer((req, res) => {
if (req.url === '/api/users') {
const users = [
{id: 1, name: "Rahul"},
{id: 2, name: "Amit"}
];

res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(users));
}
});

server.listen(3000);

▶️ Test

πŸ‘‰ http://localhost:3000/api/users


✅ Output

[
{"id":1,"name":"Rahul"},
{"id":2,"name":"Amit"}
]

πŸ“Œ From your Node PDF:
πŸ‘‰ JSON APIs are core of modern apps


πŸ§ͺ πŸ”₯ LAB 8: Handle POST Request (Send Data)

🎯 Goal

Receive data from client


πŸ› ️ Code

const http = require('http');

const server = http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/data') {
let body = '';

req.on('data', chunk => {
body += chunk.toString();
});

req.on('end', () => {
console.log('Received:', body);

res.writeHead(200);
res.end('Data received');
});
}
});

server.listen(3000);

▶️ Test using terminal

curl -X POST http://localhost:3000/data -d "name=Gyan"

✅ Output

Received: name=Gyan

πŸ“Œ Same event-driven concept from PDF


πŸ§ͺ πŸ”₯ LAB 9: Create Simple Routing System

🎯 Goal

Handle multiple pages


πŸ› ️ Code

const http = require('http');

const server = http.createServer((req, res) => {
if (req.url === '/') {
res.end('Home Page');
} else if (req.url === '/about') {
res.end('About Page');
} else if (req.url === '/contact') {
res.end('Contact Page');
} else {
res.end('404 Not Found');
}
});

server.listen(3000);

▶️ Test

  • /
  • /about
  • /contact

🧠 Learning

πŸ‘‰ This is basic Controller logic (MVC)


πŸ§ͺ πŸ”₯ LAB 10: MINI PROJECT (Frontend + Backend)

🎯 Goal

Real system: User List App


πŸ“ Project Structure

lab10-app/

├── server.js
├── index.html

πŸ› ️ Backend (server.js)

const http = require('http');

const users = [
{id: 1, name: "Rahul"},
{id: 2, name: "Amit"}
];

const server = http.createServer((req, res) => {
if (req.url === '/api/users') {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(users));
} else {
require('fs').readFile('index.html', (err, data) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(data);
});
}
});

server.listen(3000);

πŸ› ️ Frontend (index.html)

<!DOCTYPE html>
<html>
<body>
<h1>User List</h1>
<ul id="list"></ul>

<script>
fetch('/api/users')
.then(res => res.json())
.then(data => {
const list = document.getElementById('list');

data.forEach(user => {
const li = document.createElement('li');
li.innerText = user.name;
list.appendChild(li);
});
});
</script>
</body>
</html>

▶️ Run

node server.js

πŸ‘‰ Open: http://localhost:3000


✅ Output

πŸ‘‰ User names displayed dynamically


🧠 WHAT YOU BUILT

LayerExample
🎨 FrontendHTML + JS
⚙️ BackendNode API
πŸ”— CommunicationREST API

πŸ‘‰ This is 3-Tier Architecture in action


πŸš€ LAB 11–20: Express + DB + Performance + Deployment


πŸ§ͺ πŸ”₯ LAB 11: Install Express.js (Industry Standard)

🎯 Goal

Move from raw Node → Express (easy routing)


πŸ› ️ Step 1: Setup Project

mkdir lab11-express
cd lab11-express
npm init -y
npm install express

πŸ› ️ Step 2: Create server.js

const express = require('express');
const app = express();

app.get('/', (req, res) => {
res.send('Hello from Express πŸš€');
});

app.listen(3000, () => console.log('Server running'));

▶️ Run

node server.js

πŸ‘‰ Open: http://localhost:3000


🧠 Why Express?

  • Cleaner routing
  • Middleware support
  • Faster development

πŸ§ͺ πŸ”₯ LAB 12: Middleware (VERY IMPORTANT)

🎯 Goal

Understand request processing pipeline


πŸ› ️ Code

const express = require('express');
const app = express();

// Middleware
app.use((req, res, next) => {
console.log('Request received:', req.url);
next(); // move to next
});

app.get('/', (req, res) => {
res.send('Home Page');
});

app.listen(3000);

🧠 Concept

Request → Middleware → Route → Response

πŸ‘‰ Like security check before entry


πŸ§ͺ πŸ”₯ LAB 13: Connect MongoDB

🎯 Goal

Add database layer


πŸ› ️ Install

npm install mongoose

πŸ› ️ Code

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/testDB')
.then(() => console.log('DB Connected'));

🧠 From your PDF:

πŸ‘‰ Data layer = persistence system (DB)


πŸ§ͺ πŸ”₯ LAB 14: CRUD API (REAL BACKEND)

🎯 Goal

Create full API


πŸ› ️ Model

const User = mongoose.model('User', {
name: String
});

πŸ› ️ Routes

// CREATE
app.post('/users', async (req, res) => {
const user = new User({ name: "Rahul" });
await user.save();
res.send(user);
});

// READ
app.get('/users', async (req, res) => {
const users = await User.find();
res.send(users);
});

🧠 This is:

πŸ‘‰ REST API (GET, POST)


πŸ§ͺ πŸ”₯ LAB 15: Test API (Postman)

🎯 Goal

Test backend APIs


πŸ› ️ Steps

  1. Install Postman
  2. Test:
    • GET /users
    • POST /users

🧠 Output

πŸ‘‰ JSON responses


πŸ§ͺ πŸ”₯ LAB 16: Authentication (Basic Login)

🎯 Goal

Secure your app


πŸ› ️ Code

app.post('/login', (req, res) => {
const { username, password } = req.body;

if (username === 'admin' && password === '1234') {
res.send('Login success');
} else {
res.send('Invalid credentials');
}
});

🧠 Real-world:

πŸ‘‰ Used in banking, apps, portals


πŸ§ͺ πŸ”₯ LAB 17: Load Balancing (Scaling)

🎯 Goal

Handle multiple users


🧠 From your PDF:

πŸ‘‰ Multiple servers share load


πŸ› ️ Concept

User → Load Balancer → Server1 / Server2

πŸ› ️ Node Cluster Example

const cluster = require('cluster');

if (cluster.isMaster) {
cluster.fork();
cluster.fork();
} else {
require('./server');
}

πŸ§ͺ πŸ”₯ LAB 18: Performance Optimization

🎯 Goal

Make app fast


🧠 Techniques (from PDF)

✔ GZIP
✔ Caching
✔ Reduce requests
✔ Minify files


πŸ› ️ Example (Compression)

npm install compression
const compression = require('compression');
app.use(compression());

πŸ§ͺ πŸ”₯ LAB 19: Deploy App (Cloud)

🎯 Goal

Make app live


πŸ› ️ Options

  • Render
  • Railway
  • AWS (advanced)

πŸ› ️ Steps (Render Example)

  1. Push code to GitHub
  2. Connect to Render
  3. Deploy

🌍 Output

πŸ‘‰ Your app live URL


πŸ§ͺ πŸ”₯ LAB 20: FINAL PROJECT πŸ”₯

🎯 Build:

πŸ‘‰ Full User Management System


πŸ“ Structure

project/
├── backend/
│ ├── server.js
│ ├── routes/
│ ├── models/

├── frontend/
│ ├── index.html
│ ├── app.js

πŸ”₯ Features

✔ Add User
✔ View Users
✔ Delete User
✔ API Backend
✔ Database Storage


🧠 Architecture Used

ConceptUsed
3-Tier
MVC
REST
Node
Performance

🎯 FINAL RESULT (YOU NOW KNOW)

πŸ”₯ Full Web Architecture
πŸ”₯ Backend APIs
πŸ”₯ Database
πŸ”₯ Performance tuning
πŸ”₯ Deployment


πŸš€ πŸ”₯ FULL PROJECT: User Management System


πŸ“ πŸ“¦ Project Structure (IMPORTANT)

user-management-app/

├── backend/
│ ├── server.js
│ ├── db.js
│ ├── models/
│ │ └── User.js
│ ├── routes/
│ │ └── userRoutes.js

├── frontend/
│ ├── index.html
│ ├── script.js
│ ├── style.css

├── package.json

⚙️ STEP 1: Setup Project

mkdir user-management-app
cd user-management-app

mkdir backend frontend
cd backend

npm init -y
npm install express mongoose cors body-parser

🧠 STEP 2: Database Connection (db.js)

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/userDB')
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));

πŸ“¦ STEP 3: Model (models/User.js)

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
name: String,
email: String
});

module.exports = mongoose.model('User', userSchema);

πŸ”— STEP 4: Routes (routes/userRoutes.js)

const express = require('express');
const router = express.Router();
const User = require('../models/User');

// CREATE
router.post('/', async (req, res) => {
const user = new User(req.body);
await user.save();
res.json(user);
});

// READ
router.get('/', async (req, res) => {
const users = await User.find();
res.json(users);
});

// DELETE
router.delete('/:id', async (req, res) => {
await User.findByIdAndDelete(req.params.id);
res.json({message: 'Deleted'});
});

module.exports = router;

πŸš€ STEP 5: Server (server.js)

const express = require('express');
const app = express();

require('./db');

const cors = require('cors');
app.use(cors());
app.use(express.json());

const userRoutes = require('./routes/userRoutes');
app.use('/api/users', userRoutes);

app.listen(5000, () => {
console.log('Server running on http://localhost:5000');
});

🎨 STEP 6: Frontend (frontend/index.html)

<!DOCTYPE html>
<html>
<head>
<title>User Management</title>
<link rel="stylesheet" href="style.css">
</head>
<body>

<h1>User Management πŸš€</h1>

<input id="name" placeholder="Enter name">
<input id="email" placeholder="Enter email">
<button onclick="addUser()">Add User</button>

<ul id="userList"></ul>

<script src="script.js"></script>
</body>
</html>

⚡ STEP 7: Frontend Logic (frontend/script.js)

const API = 'http://localhost:5000/api/users';

// Load users
function loadUsers() {
fetch(API)
.then(res => res.json())
.then(data => {
const list = document.getElementById('userList');
list.innerHTML = '';

data.forEach(user => {
const li = document.createElement('li');
li.innerHTML = `
${user.name} (${user.email})
<button onclick="deleteUser('${user._id}')">Delete</button>
`;
list.appendChild(li);
});
});
}

// Add user
function addUser() {
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;

fetch(API, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name, email})
}).then(() => loadUsers());
}

// Delete user
function deleteUser(id) {
fetch(`${API}/${id}`, { method: 'DELETE' })
.then(() => loadUsers());
}

// Initial load
loadUsers();

🎨 STEP 8: Styling (frontend/style.css)

body {
font-family: Arial;
padding: 20px;
}

input {
margin: 5px;
padding: 5px;
}

button {
margin: 5px;
padding: 5px;
background: green;
color: white;
}

li {
margin: 10px 0;
}

▶️ STEP 9: RUN PROJECT

Start Backend

cd backend
node server.js

Start Frontend

πŸ‘‰ Open:

frontend/index.html

✅ FINAL OUTPUT

✔ Add User
✔ View Users
✔ Delete User
✔ Data stored in MongoDB


🧠 ARCHITECTURE YOU BUILT

Frontend (HTML + JS)

REST API (Express)

Database (MongoDB)

πŸ‘‰ This is exactly 3-Tier Architecture from your PDF 

No comments:

Post a Comment