Another library that I find very useful is the A.P. Status Codes, which sets up these status codes
as constants.
So instead of hard coding, we will import the constants from the library and then apply them in our
responses.
Now that is my personal preference.
So technically, you don't have to do it.
So if you still prefer just hard coding here, these number of hours, you can definitely do so.
But since I'm going to add it to our application, therefore I'll go over the details and effectively,
in my opinion, it provides more consistency in the long run.
I have found that there's less bugs and it's also easier to read and manage the application.
If you want to find out more info, here's the link.
And again, the main idea has fallen where essentially you install a library app that is clear and then
you import the constants.
So for example, for your one, we would get created and then we would set it up in response.
So again, that way, I don't need to remember the status codes.
And it's easier for me to read when I'm looking at my code.
I right away, I understand what the response is doing.
Again, my preference, you don't have to do part.
In my case, I will install it.
So we start over here.
We get the install command, which is also in Read Me Van Copy and paste the library online.
I want to set it up in two places.
I want to set up in the off controller as well as the air handler.
And then at the very end we'll set up default are which at the moment might seem like a useless thing.
But since we'll be setting up Mongo reserves in the following videos, we might as well knock it out
right now.
So let me go back and start up the server and line again.
We're looking for low status codes, import from the library and online.
We're looking for a constant.
So where we have the response, we go with status online, for example, 400 we go status code, that's
the import.
And then OK.
And again, you can read all the contents over here and which status code they reference.
So if we go back looking for off controller and I'm going to go and import more than four status codes
from and then of course, the library, the status codes and then instead of two on one, I'm going
to go here with status codes, status and codes.
And then that, OK, let's say that.
And then once we have this in place now, let me go to my error handler and I want to do the same thing
where essentially I'll be disappointment.
Well, copy are very important here.
And like I said, instead of just sending the response in this way, I'll set up the default error response.
Essentially, an object with two properties a status quo property as well as the message.
And again, like I said before the moment, it might seem like a useless thing brought in a few videos.
When we add more code, you'll see why we're doing that.
So let's start over here by creating the object.
So default and error.
If you want to add sponsor, you can definitely do so in my case.
We're going to go with default.
Are now still console.log error that still stays the same.
And then, like I said, there's going to be two properties status code.
And for the time being, I'm just going to go with status codes DOT online 4500, which is going to
be our default error, which you're looking for is internal and then server error.
And notice how they right away nicely suggest the constants for you.
So that's the 501.
And as far as the message, let's set up the property first.
So let's say here message and that will be equal to a string.
Something went wrong.
Try again later.
And now where I have that response, instead of setting this up as five hundred, we're going to go
with default error.
That's the object that and then the status code and eventually will do the same thing over here, where
a message will be equal to a default error and message.
But while we're still setting up the Marash errors, I actually want to send back default problems because
it's going to be easier to manage that instead of the console.
So that way we can right away get the full response in The Postman and I can clearly show you which
properties we're looking for.
So, yes, eventually this will be equal to default error and then the message.
But for the time being, we'll still be sending back the error that we're getting as the first argument.