Not Found Handler
The Not Found Handler component provides a centralized and consistent way to handle unmatched routes.
Installation Guide
npx servercn-cli add not-found-handlerThis component requires the API Error Handler component to function correctly.
Basic Implementation
The Not Found Handler is a middleware that catches all unmatched routes and throws a standardized 404 error.
Usage Example
The Not Found Handler must be placed after all routes but before the error handler in your Express application.
Response Format
When a route is not found, the handler returns a standardized 404 response:
The exact format depends on your ApiError and ApiResponse configuration.
How It Works
- Route Matching: Express processes routes in the order they are defined
- Catch-All: The Not Found Handler is placed after all defined routes
- Error Throwing: When a request doesn't match any route, it reaches the Not Found Handler
- Error Propagation: The handler throws an
ApiErrorwith a 404 status code - Error Handling: The global error handler catches the error and returns a standardized response