All right, and once we have the not found middleware in place, we want to do the same thing, we the
middle way.
And let me just briefly kind of go over the biggest differences between the two.
And most likely, once we start setting up the controllers, we'll swing back because there's going
to be easier for me to show you.
But effectively the main difference is that not found one will be looking for requests that do not match
any of our current route.
Correct.
Like I just said in the last video.
But when it comes to the arrow handler, it's actually looking for errors that are happening in our
existing world.
And you'll see that in a second because we'll throw an error here in our home run.
Therefore, we need to follow the rules of expression, and the rules for the error handler are following
where essentially we'll do the same thing set up a function error handler in the middleware folder will
create that as a function.
But in this case, we're looking for four parameters.
So right away, Express will know if you're looking for four parameters.
That means that first one will be error, so it will pass in that error non-market.
In the beginning, we'll just log it just so we can see whether everything works, and we're going to
go with pretty generic response where we'll go with Materne 500.
So status five hundred and nine in the JSON we'll just set up.
There was an error.
Now we still want to import that.
We still want to use Jass extension, and I'm pretty much going to stop leaving this step over here.
We'll have to repeat that to pretty much every time as we're working here in node line, I think at
some point becomes self-explanatory and we want to place this one last one.
And again, it's going to be easier for me to show you once we have some async errors already in our
controllers.
So that's why I'll put down one on ice for now.
But please, please, please do place this one last one, even though the moment it might not look like
a big deal.
And if you watched my note express course, eventually our error handler pretty much is going to look
like the one we were using throughout the course.
The one that we were taking from project to project.
But in this case, we'll set up everything from scratch.
So yes, we'll start with a very basic one.
And then each and every time we have that custom mongoose error, that's when we'll handle that.
So this is really up to you.
If you want to save yourself a little bit of time, you can copy and paste the code from there and you're
good to go.
If not, we can build it together because that's essentially what we're going to be doing.
And yes, the last step over here showcase with a sync errors.
So like I was saying, once we already have some logic in the controllers, it's just going to be easier
for me to showcase the difference between the two.
We're not found is going to be looking for all the requests that do not match any of our current route.
And the error handler one will handle the errors that are happening in the existing route.
And it might not sound like a big difference, but trust me, it is a big one and therefore we want
to place the erosion all the way at the end.
So let's actually go through the steps.
We want to create the error handler one.
Let's just go here with error and then hyphen and.
Now that is going to be jealous.
And as far as the function, I'm going to go again with the long name because my preference in the server
is to have these names like this.
Basically, I can do the auto import in that case as well.
And the reason for that is because that clearly signals to me what is happening.
Of course, this is an export default.
So technically I can name it whatever I want.
But in my case, I like this middleware part because that way I can clearly know what the function is
doing.
And then since I set that up in the file in the non-farm, I can right away use the auto import, as
always.
That is just my preference.
You don't have to do that.
So that is just a side note.
And therefore, here I'm going to go with Konst and then error and lower and lower and lower.
In Delaware, like I said, this function is going to be looking for for things.
So the error first enteric virus and then the next.
So now Express announced, OK, so this is going to be a error handler and then the first step is just
comes along in the air and then we want to go with some kind of generic response.
So whereas and let's set up a status that still will be 500 basically a general server error.
We'll go here with 500 and then let's add a JSON one and then let's set up a message and we're going
to go with what was the message over that wasn't to double check.
So there was an error.
So that's the one that I said might as well use that one is also there was an error.
And then let's explore this one.
So export.
Default, and then we're looking for error handler.
And now let's hop back to Sergius, look for the ninth problem and right after, not again, we're going
to go with that use and then we'll pass in the error handler mature and we just need to make sure that
we move this one down.
And it looks like that in this case, it actually right away also added the jazz extension as well,
which is really, really cool.
So now we can test that out, and the way we'll do that is in the home run.
Effectively, we'll throw the error.
So say throw new error.
And I mean, the message here is really unimportant because we already have the response in place.
There was an error.
So I just say here error so we can save ourselves a little bit of typing.
So let's go back.
Now I'm not going to be looking for for it's not shallow.
The Iran does not exist.
Actually, I'm going to be looking for a home Roth, and we should see there was an hour.
And if that's what you see on a screen, beautiful, we're moving in the right direction.
And then in the terminal, you should see the error and notice that is coming from the server address.
And with these two middleware in place now, it can start setting up the connection to MongoDB.