Why use asyncHandler in Express controllers?
Express 4 does not catch promise rejections from async controllers. asyncHandler wraps with Promise.resolve(fn(...)).catch(next), so rejections go to the error handler. Without it, errors are swallowed and the client hangs.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Writing Clean API Controllers in Express
Parse the request, call a service or model, send the response with the right status code, and handle errors with ApiError. Keep it thin. Move complex logic to a services layer. Use asyncHandler so rejections go to the error handler.
Yes for anything non-trivial. Controllers orchestrate; services do the work. Services are reusable and testable. Without them, controllers become long and hard to test.
Use a serializer (a toUserDTO function) that picks only the safe fields. Never return passwordHash, JWT secrets, or internal IDs. Apply the serializer before sending the response.
Still have questions?
Browse all our FAQs or reach out to our support team
