We need to predict the order of Express middleware execution for three different scenarios:
Given an Express application with multiple middleware functions, we need to determine the sequence of console.log outputs when making requests to:
GET /
GET /other-resource
GET /not-found
The key challenge is understanding how Express middleware flows through the application, including error handling middleware and route-specific middleware.
Let's break this down into manageable steps:

First middleware ('/'): Throws "First" error
Second middleware (global): Logs and continues
Third middleware ('/other-resource'): Specific to path
Fourth middleware ('/'): Throws "Fourth" error
Fifth middleware (error handler): Processes errors
Sixth middleware ('/other-resource'): Path specific
Seventh middleware (global): Sends response
Eighth middleware (error handler): Final error handler
Output:
First
Fifth
Sevent
This sequence occurs because:
The request starts at the first middleware (First), which creates and passes
an error via next(error)
When an error is passed, Express skips all regular middleware until it finds
error-handling middleware
The fifth middleware is an error handler (it has 4 parameters), so it executes
and calls next()
Since fifth didn't pass an error to next(), Express resumes normal middleware flow
The Seventh middleware executes and sends a response with res.send('Message')
Think of this like a factory line where a quality control station
(First) finds a defect and sends the item to a repair station (Fifth).
After repairs, the item returns to the normal assembly line (Seventh).
Output:
First
Fifth
Sixth
Seventh
This sequence happens because:
Like before, First creates an error and Fifth handles
After Fifth calls next() without an error, the request continues normal processing
Since the path is '/other-resource', the matching route middleware Sixth executes
Finally, Seventh executes and sends the response
This is similar to Scenario #1, but the item needs to go through an additional
specialized station (Sixth) because of its type ('/other-resource').
Output:
First
Fifth
Seventh
This sequence is identical to Scenario #1 because:
The path doesn't match '/other-resource', so the Sixth middleware is skipped
Everything else happens the same way as in Scenario #1
The middleware order prediction reveals several important Express concepts:
Think of middleware paths like a building's security checkpoints. The '/' path is like the main entrance - everyone must pass through it. More specific paths ('/other-resource') are like specific floor access after the main entrance.
Error handling in Express is like a building's emergency system. Once an alarm (error) is triggered, normal operations (regular middleware) are bypassed, and emergency protocols (error handlers) take over. One detail worth noting is that even though there's a Fourth middleware that throws an error, we never see its log because the error from First causes Express to skip it. Similarly, Second and Third are skipped due to the error from First. The Eighth error handler is also never reached because a response is already sent by Seventh. Once a response is sent, the request-response cycle is complete, and no further middleware (including error handlers) will execute.
Understanding middleware order is crucial for:
- Authentication systems (checking credentials before protected routes)
- Logging services (capturing request information)
- API rate limiting (controlling request frequency)
- Request preprocessing (formatting or validating data)
// Authentication middleware example
app.use((req, res, next) => {
if (!req.headers.authorization) {
next(new Error('Unauthorized'));
}
next();
});
// Logging middleware example
app.use((req, res, next) => {
console.log(`${req.method} ${req.path}`);
next();
});
For more complex applications, consider:
- Middleware chaining for complex operations
- Route-specific error handlers
- Conditional middleware execution
- Asynchronous middleware patterns
Remember: Middleware order is like a assembly line - each piece must be carefully placed to ensure proper processing of requests.