So what's the difference between the 404 middleware?
Basically, the resource not found middleware and the error middleware.
And essentially, I like this explanation the best.
Not found errors are going to be triggered If there is a request for a resource that doesn't exist.
Something we're going to cover again one more time in one second.
But when it comes to the error middleware, it gets triggered by the existing root.
And this is probably confusing because the error one is the last one.
So quite often students ask, Hey, but if I have, for example, invalid request, how come it gets
triggered over here but it never makes it to the error one.
And again, the reason for that is because of this middleware will take care of the requests which don't
match anything in our server.
But this one, the error one is going to get triggered by our existing controllers.
Effectively, it's going to be a valid request and the error is going to pop up when we try to implement
the functionality.
So let me show you a good example first.
Again, let's navigate to our Thunder client and one more time, let's make this request.
So notice if we go to jobs with an extra S, we'll have this 404.
So if the resource does not exist, yes, we'll trigger that 404 middleware.
Now let's go back to the regular jobs request.
So this should yield the list of jobs and back in the server.
Let's make a typo over here instead of the request, let's say I want to log, but instead of jobs again,
I'm going to go with extra s, Let's save it.
You'll notice that actually we don't trigger the error yet, but if I go back to get all jobs and again
send a request, I'm going to get back this 500.
So notice.
Again, the error middleware gets triggered by our existing root.
So in this case, the URL, the method, all of those things were correct.
The reason why we have 500 is because in here we had the error and essentially we'll have two types
of errors.
We'll have errors that are there by mistake.
So let's say again, I just mistyped something and therefore I trigger this error or we'll have errors
that actually we set up.
So what am I talking about?
Notice over here this get single job.
So we're sending back this response of 404.
Correct.
But alternatively, here's what we can do.
I can throw a new error.
So if the ID does not match, blah, blah, blah, blah, blah.
Instead of sending the response of 404, I will trigger the error, which in turn will trigger the error
middleware and then we'll send back the error response.
And if this is confusing again, we will build this a little bit later on.
But just to give you an idea, let me go back to the get single job and instead of.
Sending the 404.
Notice the moment I type.
Throw new error.
Pretty much everything gets grayed out over here.
Why?
Well, because this code is not going to run.
And let's write the same message.
No job with that ID.
And essentially, the moment we have this line of Code Express will handle that error right over here
in our error middleware.
So let me showcase that.
Remember, every time we restart the server, we set up that array from the scratch and therefore mean
most likely this.
It is going to be wrong.
And notice I click on send and I'm not getting the 404.
Actually, I'm getting the error response because again, this middleware is going to get triggered
in existing routes where 404 gets triggered if the resource is not available on the server.
Hopefully that is clear.