All right.
And before we set up the controllers and our first router, I also want to add two more Middlewares,
one for NotFound requests.
And the second one is going to be the error middleware and then in the next video.
I'll discuss the differences because this is a very common question in the course Q&A.
So first of all, let's navigate to a server.
And after all our requests.
So again, right here where I have the port, I want to go with app dot use.
And if we provide a star over here, essentially this is going to apply to all of the requests.
So notice we're not using any kind of method.
So in here I say app dot use, which means any method.
And as far as the URL is going to be all the URLs because we have the star and then we need to provide
here.
Rack and raise.
So that's our response.
And essentially, I just want to go with resort status and 404.
So pretty much every time the user is going to try to access some kind of resource that we don't have
on our server, we're going to send back 404 and then Dot and Json and we're going to go over here with
message not found.
Now before we test out our middleware.
I know it's a little bit confusing probably, but let me comment this one out and let me just showcase
that.
Express already sends back the error message.
So let me go to all routes.
Again, it doesn't really matter which request you're making.
In my case, I'm going to go with get request.
That's going to be the easiest one and just change something about this URL.
Add an extra s and you'll notice this response.
404 not found.
So this is what the Express is sending back.
And effectively in our case, we want to change that.
We want to set up our own.
Not found middleware again.
It needs to be right after all of the routes.
So basically, if none of these routes match because the way the Express reads, the routes, the order
matters, and then if none of those routes match, then we simply provide this response.
So I'm going to go back over here to get all jobs.
Let's send it here.
And now notice I'll have the same status code 404, but the response is going to be in the Json format,
and I'm going to send back the message not found and also in Express.
We can set up a error middleware, which is very useful when we want to handle errors.
And like I mentioned before, when we worked on the front end.
Handling errors is going to be big part of the application.
Now, one rule this middleware has to be the last one.
And yes, in the following video, I'll discuss the differences between the 404 and the error middleware,
but make sure it's the last one.
And as far as the controller, it's going to have access to four parameters.
So how is that going to look like?
We'll go with App.use.
We're not going to provide any kind of URL.
And in here, like I said, in the controller, there are four parameters and the first one is the error.
So that's where we'll see the actual error.
Then req res and also a next one.
And as far as the next we'll talk about a little bit later.
I mean, again, my expectation is that you are already familiar with these things.
But if anything, we'll talk about this next one a little bit later when we have a different middleware.
So now let's go with Log.
We'll definitely log the error.
That's something very useful as far as the response will go with dot status.
And for now, let's just go with something generic Again, these things will change, but for now we'll
go with the rest Dot status 500, which just means that there was a generic server error.
And then as far as the message, we'll go with something went wrong.
And then in the following video, like I said, I'll talk about the differences between these two,
the 404 and error, and also we'll test out our error middleware.