In this video, Emmanuel Henri adds the four endpoints needed to get your API started. He adds the GET, POST, PUT and DELETE calls to the server.
- [Instructor] Any application, back end or front end needs routes in order to be able to call a URL and get something back in a web application. You can call a route and go to a specific page or you can also use routes to define your endpoints in an application. We'll start working on end points. And we'll also start using postman to test our end points are working properly. So let's get to it. So the first thing I want you to do is go inside of our routes and we're already inside of it if you don't have it open, it's inside of the routes folder, inside of the source folder and it's called crmRoutes.
And what we'll do is create all the routes that we need. So in order to create a route, we'll create a function called routes. Like so. And we'll pass app, inside of our routes function. And the reason why we're doing that is because we're going to use the routes function inside of our index in order to pass the endpoints that we'll just create. Simple as that, okay. So we'll create a few routes. The first one and we're leveraging the variable that we just created.
So app first one will be for the specific address or URL contact. Like so. And inside of that URL we'll get a specific get call. So we'll do a dot notation like that. So just for your information, this is the same as if I did this, but because we're going to create multiple calls for that specific route, we'll just put them on the second line, like so, and create another one if we need another one.
So let's go ahead and do get call, which will take a request and a response as the variable, and then do something specific with that. So what are we going to do with that specific call and let's just minimize the amount of space that we have in between our parentheses. We're going to do a response and we'll send the response GET request successful!!! So for the time being, what we're going to do is print out a message whatever you call it from, postman or from the web, we're going to simply do a response like this and send that response to whatever devices calling for that get call.
Okay. And then we'll do the same for a post call. And the post is still related to the contact URL. And I'm just moving stuff around. So basically, when we do a contact or get call for the contact URL or a post call, this is what we're going to get as a response from the get and the response that we're going to code in the second will be the one that we're getting when we do the post call here. So let's do the same and let's just copy some of that code so we don't repeat.
So copy from basically both parentheses, like so, and just post it, right after post, like so. And the only thing we're going to change is POST request successfull!!! Like so. Okay. So we need now to do also a second URL. So in our application we're going to make several types of call. So we're going to make a get call which will give us all the contacts or will post a new contact which will use this URL as well.
But we'll also need sometimes to have specific contacts. Or to update a specific contact. Or to even delete a specific contact. So this is what the next route will be used for. And we'll use the same syntax and let's just copy and paste some of that above and when you copy and paste, please be careful to change all the code accordingly because this is where most mistakes happen. When you copy and paste and you don't change something then, your application will be um, impacted by this.
So let's go ahead and do that. And instead of just contact, we'll do a forward slash and then we'll do colon contactId. And for that, we'll get the put and we'll do a delete call. Like so. And, let's go ahead and change. This is a put request and this is a delete request. Let's do a semi colon here and there.
Again, don't forget that this particular function here is related to this and this particular function is related to this and so on so forth. So this one is related to this particular route. So make sure that you put the semi colon at the end of this block and at the end of this block here. Like so. Okay. So the last thing that we need to do is make sure that we export that particular function otherwise we won't be able to use it anywhere else in our application.
So let's go ahead and export default routes. And again this is ES6 syntax. Save that and let's go ahead inside of our index and import our function. So we need to import routes from and we are importing from the source folder inside of the routes folder and then crmRoutes.
Like so. And then all we need to do at this point is to run it. So we are running the function routes and guess what, we're passing app to it. So therefore, once we run this application, we're going to pass express to it and be able to basically have those api calls available. So let's go ahead and save that. We don't have any errors here. So let's go ahead and test it within Postman.
So if your don't have Postman, open go ahead and open it. And all we have to do at this point is go ahead and enter the URL. So in this case it's localhost and as you can see, I've done this before with my account, with various test here. So the one that we need to do is localhost:3000/contact and do a get call. So let's just go back to our code very quickly to understand what we're doing. So we're calling and let's go inside of crmRoutes.
And what we're doing, we're doing a get call, through the contact URL and the base URL is localhost:3000, which is what the server created for us and we'll do the get call. We should get a response, GET request successfull!!! So let's go ahead and do that. So get and then send and this is how postman works. It's very simple and we'll use different ways of doing get calls, put calls when we create more stuff inside of our application.
But for the time being, this is all we need to do. Send, and then we get request successful!!! Okay. So let's go ahead and do a post. Same thing. Send again, we get a POST request successful!!! Let's go ahead and do the same thing for put and let's just enter whatever we want right now, we can enter anything and it's still going to get us the right call, PUT request successfull!!! So let's do a delete call as well with a fictive ID again, click send and DELETE request successfull!!! Congrats! So now you've got four working end points.
There are very basic responses when you call them, we don't have anything special or data attached to those requests, but it's a start.
- Setting up a project and a server
- Setting up a database and schema
- Creating POST, GET, PUT, and DELETE endpoints
- Serving files