A route handler is the function (or functions) that ultimately respond to a matched request. This lesson covers the different ways Express lets you attach one or more handlers to a single route.
One or Many Handlers Per Route
The simplest route has a single handler function. But Express also accepts multiple handler functions, or an array of them, letting earlier ones act as route-specific middleware before the final one sends a response.
This is different from application- or router-level middleware because it's scoped to exactly one route definition, useful for logic that truly only applies there.
app.get(
'/orders/:id',
validateOrderId, // runs first
loadOrderOr404, // runs second
(req, res) => { // final handler
res.json(req.order);
}
);
Each function calls next() to pass control to the next one in the list, exactly like any other middleware chain.
Most real handlers need to await a database call or external API. Wrap the async logic so any rejected promise reaches your error-handling middleware instead of crashing silently.
Forgetting next() in a non-final handler, leaving the request hanging with no response.
Copy-pasting the same middleware combination across many routes instead of extracting a reusable array.
Letting an unhandled promise rejection inside an async handler crash the process instead of reaching the error handler.
Putting business logic directly in the route handler instead of delegating to a controller/service.
Key Takeaways
A route can have one handler or a chain of several, each with (req, res, next).
Route-scoped middleware (like a validation function) only applies to that specific route.
Reusable handler arrays reduce duplication across similar protected routes.
Wrap async handlers so rejected promises are forwarded to your error-handling middleware.
Pro Tip
Standardize one asyncHandler wrapper across your entire codebase early, retrofitting error handling into dozens of unguarded async routes later is far more work.
You now know several patterns for composing route handlers. Next, learn how to manage multiple API versions in the API Versioning lesson.