Now,
in the last lecture,
I introduced this 4 0 4 page
if a restaurant wasn't found.
And in general,
we added a lot of key features
which you'll need for many websites.
But we also have another of case where
some resource might not be found.
What if I manually entered this URL here
and I have a typo,
and instead of restaurants with an S at the end,
which would be correct.
I entered restaurant.
If I now it enter,
I get this ugly error page here,
which is not what we want.
We want to keep the user on our website
and show a custom error page.
Now that actually would be another 4 0 4 scenario
because we have a page not found case here.
It's just not that a restaurant wasn't found,
but an entire page.
We simply have no route for this
URL
and therefore I also want to show this
4 0 4 page not found fallback page here.
Now,
how could we make that work?
Of course,
we could add a new route anywhere.
Let's say at the bottom,
where we look for
get requests to slash restaurant,
and then I could handle this exact case.
But what if the user entered
restauran without the T and the S?
You see where does this leading?
We can't handle all the possible
invalid URLs the user might've entered.
Instead,
what we would want down there is a
general
catch
all
route.
That catches all requests that haven't been handled
by other routes up to this point.
And whilst there are different ways of making this work,
the most common form of handling this
is to add our own custom
middleware.
Now,
for that recall
that middleware
was
something like we have it up here.
When we use the use method on app,
and we then have some
helper
functionality that executes on every incoming request.
And that's important.
That's what I explained early in the course.
Middleware
is
some
functionality that executes
on
multiple incoming requests,
or maybe even every incoming request.
And very often it's not the last step
in handling that request,
but only one step of many.
Now we want to add our own middleware
that handles all the requests that haven't
been handled up to this point,
and sends back this 4 0 4 page
for all of them.
And the important thing now will be
that we don't add our own middleware here
next to the other middleware.
But instead at the bottom of this app JS file
right before we listen.
Now, why at the bottom?
Because all incoming requests are basically
funneled through
all these middleware functions
and all these routes.
So if a new request reaches our server,
then it is first funneled through these middlewares
and then it checks the different routes
to find the one route that should handle it.
And of course,
we only want to send back our standard 4 0 4 page,
if no route handled the that request,
otherwise we would send back a 4 0 4 page for
every incoming request
and we don't want to do that.
Therefore this 4 0 4 handling
Rick middleware should come at the very end here.
And that now just poses one question,
how do we add our own middleware?
For this we again,
use app use
because this allows us to register a function
that executes for
every incoming request.
And we can now pass our handling function
directly to app use.
App use also would allow us to still add
a URL,
which acts as a filter.
So that we say
only handle the requests that started with
slash
admin,
for example,
and other requests would be ignored,
but I don't want to have any filter here.
And therefore we just passed this
handling function like this.
Now,
this function is the same kind of handler function
as we add in all the routes.
It receives a request
and a response
and also a third object,
but I'll come back to that later in the course,
and now therefore we can still render a response here
with the res render
and renders 4 0 4 page.
And if we write it like this,
then this will kick in whenever we have a request,
that's not handled by any of the other routes.
And hence, if we now save this,
if I reload this invalid URL,
I now get my page not found page.
If I go to a valid page though,
I see that page instead.
So it really only kicks in if we entered something invalid.
And that is also a feature
we typically want to add to all our websites,
because we want to show some standard error page.
If a
invalid URL
was entered.