Express error handling middleware
Express error handling middleware
This is express default error handling. Here show where the error starts the stack
of the error
To show custom message to user we use custom errror handling middleware
Or
Because here next() will search the next non error handling middleware. SO it
founds the next one and executes it .
See this
Again
We pass a parameter in next . Now it will search for the next error handling
middleware
error
But if there is no next error handling middleware then the express default error
handling middleware will execute
See the ex
Here when error generated, first middleware 1 will execute. This throw an error
with custom message. This will search the next error handling middleware just like
next(err) but with custom message .So the middleware2 executes.
And as there next with parameter and no other error handling middleware so the
express default middleware executes with custom message
The output in console and in page:
Error middleware 1
Error middleware 2
But if
output in page
Output in console:
Error middleware 1
Error middleware 2
Page not found middleware
ExpressError.js:
index.js
in console
Error middleware 1
Error middleware 2