Building Web Applications

Learning Objectives

Section 21.1: How the Web Works?

Request-Response Model

Web Architecture

Client (Browser)
        |
        | HTTP Request
        ↓
Server (Node.js/Express)
        |
        | Database Query
        ↓
Database
        |
        | Results
        ↓
Server
        |
        | HTTP Response
        ↑
Client (Browser)

Section 21.2: HTTP, the Web Protocol

HTTP Methods

GET     - Retrieve data
POST    - Create data
PUT     - Update data
DELETE  - Delete data
PATCH   - Partial update

HTTP Status Codes

2xx - Success
  200 OK
  201 Created
  204 No Content

3xx - Redirection
  301 Moved Permanently
  304 Not Modified

4xx - Client Error
  400 Bad Request
  401 Unauthorized
  404 Not Found

5xx - Server Error
  500 Internal Server Error
  503 Service Unavailable

URL Structure

https://api.example.com:8080/users/123?page=1&limit=10#section

|-------|-----|------|--------|-------|-------------|----------|
Protocol|Host |Port  |Path    |Query  |Parameters   |Fragment

Section 21.3: HTTP Status Codes & URL

Common Status Codes

// Success
200 - OK: Request succeeded
201 - Created: New resource created
204 - No Content: Success, no response body

// Client errors
400 - Bad Request: Invalid input
401 - Unauthorized: Authentication needed
403 - Forbidden: Access denied
404 - Not Found: Resource doesn't exist

// Server errors
500 - Server Error: Internal problem
503 - Service Unavailable: Temporary problem

Query Parameters

/search?q=javascript&sort=recent&limit=20

Extract query values:
let url = new URL("https://example.com/search?q=javascript");
let query = url.searchParams.get("q");  // "javascript"

URL Encoding

// Special characters must be encoded
let search = "hello world";
let encoded = encodeURIComponent(search);  // "hello%20world"

// Build URL
let url = "https://api.example.com/search?q=" + encoded;

Section 21.4: From Web Sites to Web Apps

Website vs Web App

Website:

Web App:

Single Page Application (SPA)

// Traditional: Each page request reloads
// SPA: Update views without page reload

class Router {
    constructor() {
        this.routes = {};
        this.currentRoute = null;
    }

    addRoute(path, component) {
        this.routes[path] = component;
    }

    navigate(path) {
        if (this.routes[path]) {
            this.currentRoute = path;
            this.render();
        }
    }

    render() {
        let content = this.routes[this.currentRoute];
        document.getElementById("app").innerHTML = content;
    }
}

Section 21.5: JSON, a Data Format for the Web

JSON Basics

// JSON is JavaScript Object Notation
let person = {
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com",
    "skills": ["JavaScript", "React", "Node.js"]
};

// Convert to JSON string
let json = JSON.stringify(person);
console.log(json);

// Parse JSON string
let parsed = JSON.parse(json);
console.log(parsed.name);  // Alice

JSON Data Types

{
    "string": "text",
    "number": 42,
    "boolean": true,
    "null": null,
    "array": [1, 2, 3],
    "object": {
        "nested": "value"
    }
}

Common JSON Patterns

// API Response
{
    "success": true,
    "data": {
        "id": 1,
        "name": "Product",
        "price": 19.99
    }
}

// Array of objects
[
    { "id": 1, "name": "Item 1" },
    { "id": 2, "name": "Item 2" }
]

Learning Outcomes

After this module, you understand:


Next Module: Module 22 - Query a Web Server