0
0
Expressframework~10 mins

404 Not Found handler in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - 404 Not Found handler
Request Received
Match Routes?
YesHandle Route
No
404 Handler Middleware
Send 404 Response
End Request
When a request comes in, Express checks routes. If none match, it runs the 404 handler middleware to send a 'Not Found' response.
Execution Sample
Express
app.use((req, res) => {
  res.status(404).send('Page not found');
});
This code adds a middleware that catches all unmatched requests and sends a 404 status with a message.
Execution Table
StepRequest URLRoute Match?ActionResponse StatusResponse Body
1/homeYesHandle /home route200Home page content
2/aboutYesHandle /about route200About page content
3/unknownNoRun 404 handler middleware404Page not found
4--Request ends--
💡 Request ends after sending 404 response for unmatched URL
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Request URL/home/about/unknown/unknown/unknown
Response Status200200404404404
Response BodyHome page contentAbout page contentPage not foundPage not found
Key Moments - 2 Insights
Why does the 404 handler run only when no routes match?
Express processes routes in order. The 404 handler is last and runs only if no previous route matched, as shown in execution_table step 3.
What happens if the 404 handler is placed before routes?
It would catch all requests first, preventing routes from running. The execution_table shows correct order with 404 last.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status when the URL is '/unknown'?
A500
B200
C404
D302
💡 Hint
Check the row where Request URL is '/unknown' in the execution_table.
At which step does Express decide no route matches and runs the 404 handler?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for 'Run 404 handler middleware' in the Action column of execution_table.
If you add a new route for '/unknown' before the 404 handler, what changes in the execution table?
AStep 4 would send 404 response
BStep 3 would show 'Handle /unknown route' with status 200
CStep 3 would still run 404 handler
DNo change in execution table
💡 Hint
Think about how route matching affects which middleware runs, referencing variable_tracker and execution_table.
Concept Snapshot
Express 404 Handler:
- Placed after all routes
- Catches unmatched requests
- Sends res.status(404).send('Page not found')
- Ensures user sees clear 'Not Found' message
- Order matters: must be last middleware
Full Transcript
In Express, when a request comes in, the server checks each route in order. If a route matches the request URL, it handles the request and sends a response. If no route matches, Express runs the 404 Not Found handler middleware. This middleware sets the response status to 404 and sends a message like 'Page not found'. This process ensures users get a clear message when they visit a URL that does not exist on the server. The 404 handler must be placed after all other routes to work correctly. If placed earlier, it would block other routes from running. The execution table shows requests to known routes get a 200 status and content, while unknown routes get a 404 status and the not found message.