specific middleware which will service that request.
So this is where we will allow it to pass through.
If not, that means that the username and
password did not match the request,
the default username and password that I am setting up.
So that means that there is an error.
So in this case, I'm going to again cause an error here,
so we'll say else, Error.
So we'll again generate an error and then challenge the client to send
in the correct authorization information, the username and password here.
So that's it.
This little bit of middleware that we have just implemented here,
authorization middleware that we have just implemented here.
Is sufficient enough to implement basic authentication within out application.
So having made these changes,
let's save the changes and then we'll see how this actually works in practice.
Let's save the changes.
And then we'll go and start our server.
Now, going to the terminal, of course,
make sure that you're MongoDB server is up and running.
Otherwise, your Express server will not start up.
So I have the prompt type npm start, and
then your Express server will be up and running.
Now, open in incognito window in your browser.
The reason why I am asking you to use an incognito window is that when you type in
the username and password then, it will be cached by your browser.
So if you use an incognito window, if you restart the browser...
Then the cache will be cleared automatically,
so this information will not be remembered.
Now what happens if you type in the username and password, it will be cached,
so subsequently when you try to access the server, the cached information will
be automatically sent in the request that you generate.
So that is why it is important to
use an incognito window just to show you that the basic authentication works.
So in your browser address bar let's type localhost:3000, and see what happens.
So when you type localhost:3000 you immediately see that your browser pops up
this dialogue on top asking you to type in the username and password.
If you don't type it, let me type in some random username and then see what happens.
So if I type in a random username and password,
then you see that the request is rejected.
I'm not allowed to access the server, and
then if the server will again say that the client is not authorized.
And so it will come back and challenge us again for the correct authentication.
So let me type in the current authentication.
So let me type in admin and the password as password.
And then log in, and you will see that now the Express
application will allow you to go in and access the default value,
which in this case is the Index.html file from that static public folder.
Now, the same thing if you're trying to access localhost:dishes
without the authorization, then it wont work.
And I'll demonstrate that to you using postman in a minute.