Cheat Sheet

How the Internet Works

Internet Basics

  • Internet — a global network of interconnected computers that communicate using standardized protocols.

  • Protocol — an agreed-upon set of rules for how data should be formatted, transmitted, and received.

  • HTTP (HyperText Transfer Protocol) — the protocol that governs how clients and servers communicate on the web.

  • Client — any device or program that sends requests to a server (a browser, a mobile app, a fetch() call).

  • Server — a computer or program that receives requests and sends back responses.

  • IP Address — a unique numerical label assigned to each device on a network (e.g., 142.250.80.46).

  • Domain Name — a human-readable name (e.g., google.com) that maps to an IP address.

  • DNS (Domain Name System) — the system that translates domain names into IP addresses.

  • Packet — a small unit of data. Long messages are broken into packets, routed independently, and reassembled at the destination.

HTTP: The Request-Response Cycle

Every HTTP interaction follows the same four-step pattern:

An HTTP request contains:

  • Method — the type of action (GET, POST, PATCH, DELETE)

  • URL — the address of the resource

  • Headers — metadata (e.g., Content-Type: application/json)

  • Body (optional) — data sent with POST and PATCH requests

An HTTP response contains:

  • Status Code — a number indicating the result

  • Headers — metadata about the response

  • Body (optional) — the data being sent back (JSON, HTML, etc.)

HTTP Status Codes

Code
Name
When you'll use it

200

OK

Successful GET, PATCH, or DELETE

201

Created

Successful POST (resource was created)

204

No Content

Success but no data to return

400

Bad Request

Client sent invalid or malformed data

401

Unauthorized

Authentication required (not logged in)

403

Forbidden

Authenticated but not permitted

404

Not Found

The requested resource doesn't exist

500

Internal Server Error

Something went wrong on the server

503

Service Unavailable

A dependency (e.g., third-party API) is down

Server Basics

Host, Port, and Localhost

  • Listening — a server is "listening" when it is actively waiting for incoming requests on a specified port.

  • Host — the network address of the machine running the server.

  • Port — a number identifying a specific process on a host. Different applications run on different ports.

  • localhost — a hostname that always refers to your own computer (127.0.0.1). Used during development.

Two applications cannot listen on the same port simultaneously.

A node:http Server

Node's built-in http module lets you create a server without any npm packages:

  • http.createServer(callback) — the callback runs for every incoming request.

  • req.method / req.url / req.headers — inspect the incoming request.

  • res.writeHead(statusCode, headers) — set status and headers.

  • res.end(body) — send the response body (must be a string or Buffer).

  • Routing with node:http requires manual if/else on req.method and req.url. Express handles this for you.

Express

Express Basics

  • Express app — an object that listens for requests and routes them to the appropriate controller.

  • Endpoint — a specific URL path clients can send requests to (e.g., /api/users/:id).

  • Controller — a named callback function that parses a request and sends a response.

  • req.query — an object containing query parameters (e.g., ?name=ben{ name: 'ben' }).

Middleware

  • Middleware — a function that intercepts incoming requests before the final controller runs. It receives (req, res, next) and calls next() to pass the request forward.

  • Use app.use(fn) to run middleware on every request.

  • Middleware must either call next() or send a response — otherwise the request hangs.

  • Order matters: middleware registered before controllers runs first.

Serving Static Assets

  • Static Assets — files (HTML, CSS, JS, images) served unchanged. A React project's dist/ folder contains static assets.

  • path module — Node built-in for building reliable absolute file paths.

  • __dirname — the absolute path of the current file's directory.

express.static() checks each request against the files in the folder. If a match is found it sends the file; otherwise it calls next().

Environment Variables

  • API Key — a secret code that verifies your identity with an external API. Never expose in frontend code.

  • Environment Variable — a variable stored outside your code, accessible in Node via process.env.

  • .env file — stores secrets locally. Add to .gitignore — never commit it.

  • dotenv — npm package that loads .env into process.env.

Why use the server as a middleman? Client-side code is visible in the browser's Network tab — API keys embedded in frontend requests are publicly exposed. The server keeps the API key hidden.

Sending Frontend-to-Backend Requests (Vite Proxy)

  • Same-origin request — a request made from a page to the same host and port that served it.

  • Cross-origin request — a request made from one origin to a different origin.

When the frontend is served by the backend at localhost:8080, use relative URLs:

During development, Vite runs on localhost:5173 (a different origin from your backend on :8080). Configure Vite to proxy /api requests to the backend:

RESTful CRUD API

REST Principles

REST (Representational State Transfer) is an architectural style for designing web APIs. A RESTful API follows these guidelines:

  1. Endpoints describe resources, not actions — use plural nouns, not verbs:

    • /api/fellows ✓ vs. /api/getFellows

  2. HTTP methods communicate the action:

    • GET – Read | POST – Create | PATCH/PUT – Update | DELETE – Remove

  3. Use IDs to target individual resources: GET /api/fellows/3

  4. Nest resources to indicate ownership: POST /api/fellows/3/posts

  5. Use proper status codes: 201 for created, 404 for not found, etc.

CRUD and HTTP Methods

CRUD
HTTP Method
Endpoint
Status on success

Create

POST

/api/fellows

201 Created

Read

GET

/api/fellows

200 OK

Read

GET

/api/fellows/:id

200 OK

Update

PATCH

/api/fellows/:id

200 OK

Delete

DELETE

/api/fellows/:id

204 No Content

Route Parameters

Route parameters are named placeholders in a URL, defined with :. Their values are available on req.params.

Handling POST and PATCH Request Bodies

Use the express.json() middleware to parse incoming JSON bodies into req.body:

Frontend fetch — include method, headers, and a stringified body:

Model-View-Controller (MVC)

MVC Architecture

MVC separates an application into three distinct layers:

  • Model — manages the data. Provides methods (an interface) for creating, reading, updating, and deleting data. Does not know about req or res.

  • View — renders the UI. In a PERN app, this is the React frontend.

  • Controller — the middleman. Parses requests, calls model methods, and sends responses. Does not directly touch the data.

Recommended server folder structure:

Model Example

A model uses a class with static methods. The data (the "database") lives inside the model — controllers never access it directly.

Controllers stay focused on HTTP concerns only:

Last updated