Welcome, to the Node.js http-status-codes package🔥 The http-status-codes package is a convenient utility in Node.js for working with HTTP status codes. It provides a collection of constants that represent HTTP status codes, making it easier to handle and check the outcome of HTTP requests. Whether you need to identify success, client errors, or server errors, http-status-codes simplifies the process of managing and interpreting these codes in your applications, ensuring robust error handling and response management.
Hello, what's up guys? My name is Puneet Bahadur Thapa and today in this video we will see status code package in NodeJS.
Yes guys, let me tell you, today we have a very awesome one, you can say, it's a minimum application.
Yes guys, where I would like to show you that how we...
Basically, what is status code?
Whenever you request a page, then we get a status that yes, it was successful or not, or there was no 404 error or whatever.
But look at this status, there are different different statuses, 200, 304, etc.
So, most of the time we don't remember the code of that status, but still we know that maybe it can be OK or it can be a server error.
So, here we have this package and they have kept its name on constant, like created.
Means, whatever you wanted to create, OK, means the page I wanted to request.
So, we have a reason phase also.
And you can see, mostly between 400 to 500, there is a problem related to error.
5000 is your internal server error.
I mean 500, we have a 400 bad requests, unauthorized when you have a token.
If you remember all this, then guys, you can see here, I uploaded a post today that I am bringing one website with admin panel for you.
I know you were waiting a lot, even though I created it in 2021, but I am going to bring an updated version of this, but with admin panel and a lot of new things will come.
You know, routers, controllers, a lot of authentication, different different things will be there.
So, when I was doing it, I thought why not we have one package called status code and tomorrow I will tell you about another package, so your life will be easier.
And as I said, you can say menu 1, where I will show you the connection with the database.
Let's start the video without any delay.
OK, so I will remove it.
I have not checked the course yet.
World Best Complete, MongoDB, HTML, CSS TypeScript.
Be sure to check it out.
So guys, I am coming to VS Code Editor.
You can create any folder.
First of all, what we need to do, we have to create a package.
Package.json file, so that I can install different packages.
So guys, first of all, you have to write npm init and then hyphen y.
Add this flag and everything will be done automatically.
No tension.
Now what we have to do, guys, I have to install Express.
Obviously, we need to install Express.
And then this code, what is this code, HTTP.
This is our code, I copy and paste.
And obviously, I will show you the connection with MongoDB.
So these three packages, one second, I wrote the wrong express, but it's fine.
You can install it.
So I go back and write x and then press.
I want to see one thing.
In package.json file, oh my god.
This was also a package already.
Guys, there is also a package like this.
It exists, so it's okay.
Now I leave it as it is.
Now I am removing it too.
Leave it guys, it has nothing to do with it.
Here I create a new file, main server.js file or index.js file.
You can create.
So first of all, to work in Express, basically to work in Node.
I have used Express.
And then let's get the Express.
Express using, we need to require.
You have Express here.
Not this one, this one.
You can use control space, this is it.
Now to work with Express, we will have to create a variable.
You can give any name, but it is a universal convention app.
So now I will call my express function with empty arguments.
So now, this is my app variable, constant variable.
It has all the power of Express.
So let's say our main homepage was awesome, but technical website.
You have to access the homepage.
So I have added the URL of the homepage.
Basically path.
And then we need to pass the callback function.
Inside this we have request and then response.
So basically what is request?
I am a user who is requesting that I want to see the homepage.
So I requested the homepage.
Look, there is nothing here.
There is a forward slash by default.
So I got these pages and I used get method here.
That's why we have this get method.
And now if I am admin here.
And if I go to its contact page.
And if I fill any data and send it, it will be posted.
Because I have to add it to the server.
So in the same way, we have a get, post, delete, patch, put, update.
There are so many methods.
You will know all that in the next video.
But today status code.
So this is basically simple.
I don't have to do anything here.
Whenever someone comes to my page, I will share a status with them.
Yes, this is absolutely right.
That's your 200.
Okay.
And then I can use a JSON here.
You can use the send method directly.
I write a message here.
Welcome to Thapa.
And then we have this technical.
That's all I have to do.
Now we have to listen to the server.
So we have one method called listen.
In this, we need to pass the port.
Which port number do you want to listen to the server?
And let me do one thing.
I will create a variable called const port.
I will say run it in 5000.
And if its spelling is wrong, I will correct it.
I will pass this port here.
And then we have again a callback.
If this is successful, I can lock it.
So I am using template literals here.
Server is running.
Now you use template literals here.
And you pass this port.
This is it.
This is my work done.
I save the controller.
Look guys, I have already installed nodemon globally.
A long time ago.
So I am hitting nodemon server.js.
So see we are getting.
The spelling of the server is wrong.
But it's fine.
But server is running at port 5000.
Means my server is working.
Means if anyone hits this route,
he will definitely get this message.
Let's try it out.
So I am going here.
We have a localhost.
The weight was 5173.
This is our 5000.
And boom guys, see we have this.
Welcome to Thapa technical.
You can do the same thing on our postman.
By default we have this postman.
I have made a new request here.
So you can see.
I have made a new request on YouTube.
Which will be my localhost 5000.
And then I will click on send.
So we are getting welcome to Thapa technical.
And then you can see in status 200.
And then OK I can see.
So this is your status code.
And this is your status.
The name of the rephrase is here.
Because they have just recently changed.
And that is your reason phrase.
Which is the text of your status basically.
So this and then this.
Now I will get it.
Code and then reason phrase.
I want.
So how will it be?
See I am going here.
So guys nothing to do.
It has been installed.
So now I have to import it.
Status code.
And then get reason.
And then phrase.
This is what it has to do.
And you can go here on repo.
Here on repo.
They have explained in a very awesome way.
Every little thing.
See how it is written here.
The function getStatusText.
Has been renamed to getReasonPhrase.
It is told in a very awesome way here.
On GitHub.
It will be fun.
Let's do one thing.
I am going here.
OK.
So at this place.
Now you will know.
The hard code we are doing.
200.
So there we are going to use the status code package.
See.
So I go here.
Const.
And now we have.
Status code.
Status.
And then code.
You have to go here and require.
OK.
I go here.
And then.
Yeah.
Obviously.
It's a status code.
HTTP status code.
Yeah.
This is it.
OK.
So now you can see.
Status code.
And basically.
I copy it.
Paste.
I am removing it for now.
Yeah.
This is it.
I am removing this code.
Const.
I am removing this code.
Perfect.
So we have our status codes.
And then get reason page.
We have got it.
So this 200.
I am removing it.
This is our status code.
OK.
Is it a codes or a code?
Yeah.
It's a codes.
OK.
And then dot.
See.
We have already got a lot of things here.
See.
So many things.
For now.
I will click on OK.
This is it.
So the moment.
I do this.
And again.
I go to my request.
And then send.
See.
I am getting the same thing again.
And if you think.
Maybe this update is not done.
Then welcome.
Again.
I will write.
I save the controller.
Over here.
And then again.
I send.
So see.
I am getting again.
Means.
I am doing this work.
Now.
Let's say.
Now you have to go to my course section.
Do one thing.
You have to go to admin.
Either.
You have to go to the server.
And server.
Obviously.
For some reason.
Now you are not getting it.
So guys.
Instead of OK.
We have internal server error.
Yeah.
This is it.
Now you.
I mean.
We wrote it this way.
So easily.
Now.
If I go here.
OK.
Here.
I am hitting a request.
And I am here.
Not over here.
On my postman.
OK.
A new request.
So I said.
Come on.
I had to give a name.
I write home.
OK.
So it's a home.
A new request.
I can add here.
Whose name.
You can give anything.
Let's say.
Server.
OK.
I am here.
I have to read the server data.
And again.
We have a.
Localhost.
5000.
And then forward slash.
We have a server.
And yeah.
If I enter.
Then you can see.
Welcome to the Thapa Technical.
Again.
But.
What we are getting is.
500 internal server error.
See.
Beauty.
And obviously.
I didn't have to send this.
This message.
Which has been sent here.
So it.
Server error.
Now see.
See.
The problem is.
The message I wrote above.
It came here.
So sometimes.
Sometimes.