Dead Ends
The Catch-All
Section titled “The Catch-All”By default, if a request makes it to the bottom of your file without matching any route, Express just shrugs. We need a designated “Dead End” sign for lost drivers.
Fig 1: 404 - Nothing to see here, except a fail whale.
Express matches routes in order. We use app.all('*') as the final safety net.
- app.all: Matches ALL methods (GET, POST, etc.)
- ’*’: Matches ALL paths.
// ... all other routes above ...
// The "Lost Driver" Handlerapp.all('*', (req, res) => { res.status(404).send('404: You seem to be off the map, friend.');});Why the Status Code Matters
Section titled “Why the Status Code Matters”You might ask: “Why not just send the message?”
If you just do res.send('Not Found'), Express defaults to a 200 OK status. To the browser (or a search engine bot), that looks like a successful page load. The user sees an error message, but the machine thinks everything is fine.
By explicitly chaining .status(404), we tell the client: “This resource does not exist.” This is critical for SEO and for API clients to know they hit a dead end.
Extra Bits & Bytes
Section titled “Extra Bits & Bytes”Express Unhandled Routes (404s) - Demo Repo
Express FAQ: 404s