In this video.
Let's see how we can implement routine inside our server.
So this where we left.
So the first thing is we need to find a way to run this particular server.
So to be able to run express server, let's follow these steps.
So step number one is let's go to the package dot JSON file and here we have a property code script.
So we are going to configure this one to use our own script.
So let's provide start here and then the value we are going to use Node to start our server.
The next segment is a file that we want to run and that is the server file because that is what we provided.
So we are going to run this server file.
So inside my package or JSON file here, I'll provide server towards JS because this is what we specified
here.
So now after that, let's save the file and now let's get back to our terminal.
So let's see how we are going to run the server.
We are going to make use of NPM run and now the property name here is called START.
So let's provide NPM Run Start.
So let's hit enter and you can see that server is running.
So now how can we implement routing?
So let's close this one and now back to the server file.
So here we go.
We are going to follow the same format as this one now going to provide the HTTP method with this endpoint.
So here we go.
Let's provide a comment here and call it as routing on the app that you got from the express function
call.
We have all the HTTP method, so let's see on the app, if you make use of dot gate, this is the HTTP
method.
If you want post, make use of post and then delete.
We have it as that and then put we have all the HTTP method.
So now let's start with the first one.
Call fetch all posts.
So here we go.
You provide gates and this gate is a function that you provide to a command.
One is a path of the URL which is post.
So it goes like this code and then forward slash.
So first slash represents the main you are URL and then slash this going to be the resource.
Then you provide the callback function inside this gate at HTTP method.
So let's use normal function call.
I'll start.
And inside the callback function we have access to requests and then the response.
So now inside the callback function here we can implement our logic.
So for the meantime, let's just send a static text to the user.
So here we go.
So make use of rest.
And if we want to send a data or a resource or response, a user uses what is called ST and not JS.
We use what is called right.
So here let's provide and says that fetch.
Our post.
Drought.
All right.
So the next question is, how can we test it?
Because if they change it to the server, let's try to restart it.
So let me clear my terminal here and then run the server again.
Now let's open in the browser and make use of looker host Let's look at the port number we use, which
is 9000 localhost, 9000 then four slash posts.
When I hit send you can see that I get back fetch all posts so you can see with express is pretty easy
to get this done.
Do you remember?
And that Node.js to get this done we need to write a lot of code.
So let me show you from previous video and that is core module four for HTTP.
You can say that to be able to send a simple text to the user unless we write these bunch of code so
it accelerates, it takes away all this abstraction from ISE for us to focus on our business logic.
How awesome express is.
So this is what we call the fetch or route.
So here are going to be fetch or post.
So let's also provide the home route, right?
So home roots going to be like this one.
So app, dot gate and then forward slash, that's the home route.
Then you provide a callback function, access to request and then response and here you go.
So for this one, let's send a simple text and let's say that welcome home, let me see home route.
So let's see if you're going to get this message called home route.
So let's go ahead and then restart our server if we see how difficult it is any time make changes to
the file unless we come back here and then restart the server manually.
But we have a solution for it.
So I will show you in just a second.
So let me shut the server and then do the restart.
So let's remove the post here and now hit send.
And indeed I have what is called the home route.
So now we are done with the two route.
So what about this one about posts here.
The difference here is the HTTP method and that is it.
So we can even copy this one.
All right, so and down here, let's paste.
It's going to be as creates post.
I see that the difference here is the HTTP method as post.
We cannot make post requests here because there is no form.
That's why we need a tool called Postman, but let's try it out inside Visual City Code and Node.js.
We installed extension core tender and this one is almost same as Postman.
It helps us to make HTTP requests to the back end.
So here, try to make sure that you have the extension called tender clients.
So if you search for ten dot client.
Now go ahead and install it because I have it already.
And then after that you have this icon here.
Click on that and click on new requests.
So here you provide the HTTP method here, which is post the URL is going to be HTTP colon.
So going to be something like this for s large forest slash looker.
Host Cullen 9000 slash post.
Now, when I hit send, you can see that I get something error here.
Meaning that this post is not found.
I have a status code 404.
Meaning this root is not found.
Meaning that I need to restart the server again.
So here we go.
Shut down the server and let's get going.
So now let's try again.
Hit send again here.
And indeed I have.
What?
Fetch or post?
I think I need to change the text here.
So back to my file here instead of fetch all I'll make use of create post routes as that so let's save
it.
And again we need to restart the server again.
So now don't worry, we will avoid that very soon.
So let's hit send and now see that I have the post route and I can also make the get requests when I
hit send to see that I get the response giving me status code 200 meaning that everything is okay and
this one determines the time I got the response.
So now we have these three endpoints.
The next endpoints are the route is going to be put request.
So let's see how we are going to do it.
So I'm going to copy this same route here and down here.
Let me paste it down here.
The difference here is the HTTP method this time around is put, but you are different.
So here unless you provide a dynamic ID here.
So here is semicolon id.
So we call this one params meaning dynamic id.
So let's see how we can get an id from the request.
Now let me change this one to put again.
Let me save the file and restart the server.
Restart the server and start and next is let's make the request.
When I hit send where we see that I get HTML meaning that the route I'm trying to request is not found
because the route says that for put it says that post and there is some ID.
So here we can provide any ID, any ID here.
So when I hit send you see that I get the post route.
So let me change the text also.
So here I'm going to be as update post route.
So here I'm going to be update.
Now let me save it and again let me restart the server, click the console and now let me restart it.
So again, let me make the request and see that update route.
So again, this is really crucial how to get an ID from your route.
And lucky for us on the request we have what is called rect params.
So let's say that we want to get the get the dynamic ID, which is equal to the params.
All right.
So here we go.
So const ID is equal to req dot params.
So if I console.log the ID here, let's see the ID we pass in here.
I want to have access to this ID inside my route so that I can use the ID to fetch a record inside my
database or my file.
So now again, unless I restart the server.
So here we go.
Let's make the request and now let's check the terminal.
And indeed I have the ID.
This ID represent what I use inside my route, which is this, and it's equal to what I provided here.
So here, if I provide, let's say anything here, so let's save it.
And now let me restart the server, click the console and now let's see.
Now let me make the request and hit send.
Now back to my terminal and see that I have anything here.
But by convention you normally use ID, so let's change this one to ID.
So here to get an id, all I need to do is to make use of dot ID, because that's what we got from here.
All right?
So now that's how we able to get the ID.
So as you move on to build the final project, we may need this params.
So take note about this.
And lastly, we need to provide the delete.
All right, so here we go.
So let me copy this one.
And now down here, the difference here also is the HTTP method is around is delete.
And now for delete we need to provide dynamic ID as same as the update.
So here slash semicolon id.
So let me save it.
Now let me change this one to delete post root.
So now let me restart the server, play the console and here we go.
So let's make the request.
Let me change the HTTP method to get and now it follows the same id.
Let me say the sum around the id is 20.
Let me hit send.
Now I get an error because this root is not found.
So instead is deletes requests.
I forgot to provide the HTTP method, so let me hit send.
And if you see that I have the delete root even though the delete and update are the same.
Right.
The endpoint you can see we have post slash column ID, which is the same as this.
So these two root are different because of the HTTP method.
Take note about this.
And lastly, we forgot to implement fetch a single post.
So here we go so I can copy this one.
And then down here I will paste it and change this one to single post.
And here it followed the same root.
The difference here is going to be the HTTP method.
Now you're going to be a single.
Single post roots.
And here we go.
So now let me make the requests again unless I restart the server, don't worry.
In this video you will see how to avoid this using a package called Nordman.
All right, so let me restart it.
And now check it out to make the requests.
Let me change this one to get.
And now let me hit send.
If you see that, I get the single route.
All right.
So these are the operation route delete update.
But there is no resource, but instead it's just returning a static text like this.
The next video we are going to avoid how we are doing the manual restarting and then avoid using this
tool and use a tool called Postman How to use post one to test our endpoints.
Let's get into that in the next video.