- [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 end points in an application.
We'll start working on our endpoints
and we'll also start using Postman
to test our endpoints are working properly
so let's get to it.
So the first thing, let's go ahead and use Postman
to test currently what we have for our get call
with the actually URL address.
So if we send a request we'll get the message
node and express server running on port 4000
which is the expected behavior.
So if we go back to the index
we see that we are calling onto this address
and we get this message.
But now what we're going to do is create specific routes
for specific endpoints and then test them in Postman.
Let's go ahead and create the routes.
You're going to go into the routes folder
and then open the crm routes file.
And we're going to start by creating the routes function
which takes an app as an argument
and this is an arrow function from ES6
and then we're going to create with the app argument
which will be passed when we use it inside of index.
We're all going to pass the routes,
so the first that we'll create is contact
and within that route we're going to create a get method
and this get method will take a request and response,
so we're getting a request and then we'll issue a response
and this will be a function
and then the response will be to send a message,
so we'll say get request successful.
And this is pretty much what we're going to do
so let me just tab these out
so we have some structure here.
So the second one will be a post request
and it's going to be very, very similar to what we've done here
so let's copy and paste this.
And the only change here is post request successful.
And then we're going to close out this particular part here
because we're going to create another route
that will be specifically for the more complex request.
So for example, app.route and the other one's going to be
/contact then we're going to pass contactId
and you want to make sure the contactId that you're using
in here is the same when we do the controllers
and you're going to understand this later on.
And then inside of this particular route,
we're going to need to do a put,
so this is how we're going to update a specific id,
so we're going to use a put method
and then what I'm going to do is the same thing
we've done of the other ones
and this is going to be put request successful.
And then we're going to create the last one which is the delete
and then let's paste again,
we have the same thing
and this one's going to be delete request successful.
So basically what we're doing here is creating
the main route for getting all of our contacts
for posting new contact, for updating a single contact
which we need to pass an id
and then delete a single contact,
which we also need to pass an id.
And these are the main routes
that we're going to work with in this API
and all we need to do after that is export default routes,
so we're exporting our function that we'll use in the index.
So let's go back to the index
and make sure that we import first.
And let's go in and import routes
from src/routes/crmroutes
like so,
and then below the port number here
we're going to use our routes function
and then pass app, which is the Express library, like so.
So by passing all this, we're passing inside
of our routes function app,
which we're using here and we're creating
all these different methods that we can use
with this specific URL, so let's go and test that.
And first let me do control (mumbles)
or you can do view terminal to get the terminal
and everything seems to be running properly.
So let's go to Postman
and then we're going to do first a get request again.
And we want to make sure that we add the contact URL
and now we get the get request successful.
Let's go the same for post, same address.
Post request successful
and then put and put we need to add some ID
so let's add some random things here
it's going to basically respond regardless
because we don't have any data yet.
Put request successful
and let's go the same for the delete method
and delete request successful.
So congrats, you've got now four working endpoints.
They're very basic responses when you call them
and we don't have anything special
or data attached to those requests yet but it's a start.