Skip to content

Dead Ends

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.

Neon retro style fail whale being lifted by drones. Text says 404.

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" Handler
app.all('*', (req, res) => {
res.status(404).send('404: You seem to be off the map, friend.');
});

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.


Express Unhandled Routes (404s) - Demo Repo

Express FAQ: 404s

⏭ Local Roads

Our main highway is getting congested. Time to build some local roads (Modules).