Skip to content
Share
Explore

Kickoff lab to deploy a clean Express server with several routes

Kickoff lab to deploy a clean Express server with several routes. Demonstrates routing, params, query strings, POST bodies, middleware, static files, and error handling.
Learning outcomes:
How Node Express servers work with Routes.
We will build this up to make MONGO DB calls.

minus

Lecture note on curl.

📘 Lecture Note: curl — Command Line URL Tool

1. What is curl?

curl = Client URL.
It is a command-line tool used to transfer data to and from servers using many protocols (HTTP, HTTPS, FTP, etc.).
In web development, we use it primarily for testing web APIs (sending GET, POST, PUT, DELETE requests).
It is lightweight, fast, and runs on Linux, macOS, and Windows.
Analogy: If your browser is a full “ship” sailing the web, curl is a dinghy—small, fast, and focused on testing routes.

2. Why do developers use curl?

To test APIs without writing frontend code.
To debug servers (check headers, error codes).
To simulate HTTP clients (e.g., sending JSON, form data).
To automate workflows in shell scripts.

3. Basic Usage

🔹 GET request

Fetch a webpage or API endpoint:
curl http://example.com

🔹 JSON API request with headers

curl -H "Accept: application/json" http://localhost:3000/health

🔹 POST with form data

curl -X POST http://localhost:3000/echo -d "msg=HelloWorld"

🔹 POST with JSON body

curl -X POST http://localhost:3000/echo \
-H "Content-Type: application/json" \
-d '{"msg":"Qapla!"}'

🔹 PUT and DELETE

curl -X PUT http://localhost:3000/item/123 -d '{"name":"Updated"}'
curl -X DELETE http://localhost:3000/item/123

🔹 Show full response (headers + body)

curl -i http://localhost:3000/health

4. Installing curl

🔹 Linux (Debian/Ubuntu)

sudo apt update
sudo apt install curl -y

🔹 Linux (Fedora/CentOS/RHEL)

sudo dnf install curl -y

🔹 macOS

curl is pre-installed.
To upgrade:
brew install curl

🔹 Windows

Windows 10+ already includes curl in PowerShell and CMD.
To verify:
curl --version

If missing:
Download from:
Or install via Chocolatey:
choco install curl

5. Verifying Installation

After install, run:
curl --version

Expected: prints version, SSL libraries, supported protocols.

6. Developer Checklist

I can run curl --version.
I can GET a page (curl http://example.com).
I can POST JSON to an Express route.
I can see headers with -i.
I can simulate PUT/DELETE for CRUD APIs.
Intent:Use to test your Express + MongoDB APIs without needing Postman or a browser.
Tomorrow we will compare curl with Postman.

info

Lecture note on GET vs POST, when to use each, and provides Express.js examples for handling URL parameters, query strings, and POST bodies.

📘 Lecture Note: GET vs POST & Extracting URL Parameters in Express.js

1. What is an HTTP Method?

When clients (browsers, curl, APIs) talk to a web server, they use HTTP methods.
The two most common in web development are:
GET – ask the server for information.
POST – send data to the server (usually to create something).

2. GET Requests

Purpose: Retrieve data.
Characteristics:
Parameters go in the URL.
Safe to bookmark and cache.
Visible in the browser address bar.
Should not modify server state.
Example URL with query string:
/search?term=icecream&limit=10

Express handler:
app.get('/search', (req, res) => {
const term = req.query.term; // "icecream"
const limit = req.query.limit; // "10"
res.send(`Searching for ${term}, limit = ${limit}`);
});

3. POST Requests

Purpose: Submit data (create, update, send forms).
Characteristics:
Data goes in the request body, not the URL.
Not visible in the browser bar.
Common for forms, logins, API writes.
HTML form example (POST):
<form action="/signup" method="POST">
<input name="username" placeholder="Username" />
<input name="password" type="password" placeholder="Password" />
<button type="submit">Sign Up</button>
</form>

Express handler:
app.post('/signup', (req, res) => {
const username = req.body.username; // parsed by express.urlencoded()
const password = req.body.password;
res.send(`User created: ${username}`);
});

Note: To handle form or JSON bodies in Express, you must use middleware:
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

4. Route Parameters (/path/:variable)

Route parameters are part of the URL itself, not query strings.
Example URL:
/hello/Amy

Express handler:
app.get('/hello/:name', (req, res) => {
const name = req.params.name; // "Amy"
res.send(`Hello, ${name}!`);
});

5. Comparing GET vs POST

6. Testing with curl

GET with query string

curl "http://localhost:3000/search?term=icecream&limit=10"

GET with route param

curl "http://localhost:3000/hello/Amy"

POST with form data

curl -X POST http://localhost:3000/signup -d "username=kirk&password=enterprise"

POST with JSON

curl -X POST http://localhost:3000/signup \
-H "Content-Type: application/json" \
-d '{"username":"spock","password":"logical"}'

7. Deliverables (Cadet Checklist)

I can explain the difference between GET and POST.
I can extract query params (req.query).
I can extract route params (req.params).
I can extract POST body data (req.body).
I tested with curl and saw expected output.
Commander's Intent: Cadets leave this lecture able to:
Explain when to use GET vs POST.
Build Express routes using params, queries, and body.
Test routes using curl or browser.
Later we will see examples with:
GET /calculator?x=10&y=5&op=add
POST /calculator (with JSON { "x":10, "y":5, "op":"add" }) so they practice GET vs POST side-by-side?

🚀 Lab 0 — Spin Up an Express Web Server (Routes Demo)

Mission Objective: Launch a production-style Express server that demonstrates common route patterns and is testable from browser and curl.
What you’ll build (today):
/ – home (HTML)
/health – health check (JSON)
/time – current server time (JSON)
/hello/:name – route params
/sum?a=1&b=2 – query string handling
POST /echo – body parsing
Static files from /public
404 + centralized error handler
Dev workflow with nodemon

1) Create the project

mkdir express-lab && cd express-lab
npm init -y
npm install express
npm install -D nodemon

Create package.json scripts (replace the "scripts" section):
{
"name": "express-lab",
"version": "1.0.0",
"main": "server.js",
"type": "commonjs",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"dependencies": {
"express": "^4.19.2"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}

✅ We use CommonJS (require) so students don’t wrestle with ESM config.

2) Project structure

express-lab/
public/
index.html
server.js
package.json

Create public/index.html:
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Express Lab</title></head>
<body>
<h1>Express Lab: Static Home</h1>
<p>Served from /public via express.static().</p>
<ul>
<li><a href="/health">/health</a></li>
<li><a href="/time">/time</a></li>
<li><a href="/hello/Cadet">/hello/Cadet</a></li>
<li><a href="/sum?a=7&b=5">/sum?a=7&b=5</a></li>
</ul>
<form action="/echo" method="post">
<input name="msg" placeholder="Type a message" />
<button type="submit">POST /echo</button>
</form>
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.