Traffic Types - HTTP Methods
Sorting by Intent
Section titled “Sorting by Intent”So far we’ve mostly looked at GET requests (sightseeing). But traffic on the Data Superhighway comes in many flavors. Express segregates traffic not just by where it’s going (path), but by what it wants to do (method).
Fig 1: Traffic Sorting Logic.
Here is the standard CRUD breakdown for our interchange:
| Method | Action | Driving Analogy |
|---|---|---|
| GET | Read | Sightseeing. Just looking. |
| POST | Create | Delivery Truck. Dropping off new cargo. |
| PUT | Update (Replace) | Road Construction. Replacing the whole lane. |
| PATCH | Update (Partial) | Pothole repair. Fixing just one part. |
| DELETE | Delete | Demolition Crew. Removing the road entirely. |
Strict Lane Enforcement
Section titled “Strict Lane Enforcement”Express is strict. app.get('/things') and app.post('/things') are two completely different roads, even if they share the same asphalt (URL).
// GET /things -> "Show me the things"app.get('/things', (req, res) => { res.send('Here is the list.');});
// POST /things -> "Take this new thing"app.post('/things', (req, res) => { res.send('New thing received.');});
// DELETE /things -> "Destroy the things"app.delete('/things', (req, res) => { res.send('Things destroyed.');});The HTML Form Limitation
Section titled “The HTML Form Limitation”Here is a pothole you will hit immediately: Native HTML Forms only support GET and POST.
If you try to set <form method="DELETE">, the browser will ignore you and send a GET request instead (because it defaults to GET if it doesn’t understand the method).
How do we fix this?
Section titled “How do we fix this?”- AJAX / Fetch: JavaScript (on the client) has no such limitation.
fetch('/things', { method: 'DELETE' })works perfectly. - Method Override: A middleware technique (often used in classic SSR apps) where we trick the server. We send a
POSTrequest but add a query string like?_method=DELETE. The server sees the note and “pretends” it received a DELETE.
For now, just know that if you are building a pure HTML/Simple Express app without client-side JS, you are mostly stuck in the GET and POST lanes.
Extra Bits & Bytes
Section titled “Extra Bits & Bytes”Express HTTP Method Routing - Demo Repo
MDN HTTP Methods