DEV Community

Discussion on: Central Error Handling in Express

Collapse
 
ajaysainy profile image
Ajay Sainy

Thanks for a useful post. One question on usage of validateUser method. How do we use that method in lets say any PUT path that validates a user?
Simply calling validateUser(req, res, next) is causing the error Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in the code below validateUser call that assumes the user is already validated.

I am guessing this is happening because express does not immediate terminates the request processing after the response has been sent to the client.

Collapse
 
nedsoft profile image
Chinedu Orie

I think your guess is right. Sometimes it happens if a return statement is skipped somewhere, so the execution continues after the first error encounter