All right so now let's dive back into our actual application now that we talked a little bit about how
the API works.
Go back here.
We installed all those those three packages.
Let's set up our basic app.
So there should be review but touch.
Yes let's open it up.
What does it C9.
OK.
Do our basic So we need var express equals.
And like I said this is going to be barebones we're not going to style it and make it pretty at a real
fun functionality.
But it will be something you can use sort of as a template for other apps that use API and will do app
equals Express.
While we're here so it's going to open up a previous app.
Let's do this one.
Copy this line.
I always forget this one.
The process that the infield up port but I'll just put this here and I'll change it to say movie app
has started OK.
And then will also require request just like I'd save it to a variable.
Ok so this express app like I said will have two routes one slash search and another one slash results
.
So you'll see a form on one page.
Fill it out and then that submission of that form will then trigger or send a request to our results
route which will then actually work with the API send a request the API that will come back and then
will display the results.
Let's start simple by taking apt get will just do slash results
request response or standard route in then in here is when we'll actually do an API call.
So when a user triggers slash results we'll actually do a search of the API.
And we're going to start by hard coding it in.
So LEDs hard code in a search like we have here.
I'm sure it will do it for California.
So this is the API call we're just going to start off by making every time.
So just to jog your memory here this is what we're looking for.
Request this giant you R-AL and then call back with error wups error response and body.
So let's try that now let's just start by just making sure our route works.
So we'll do let's do that send it low.
It works just like that at startup per server.
Ok movie epos started.
Let's now go visit preview.
So we don't have our route route set up but we should have Slash results and we get hello.
It works.
And I'll just open that up in a new tab and get rid of this here.
OK.
So rather than seeing that we want to see our movie data and we'll start by just seeing all of it.
So I want to see this but when I go to slash results so we're not dealing with HDMI yet that will be
next.
But we just want to make sure our API is connecting and that's what I usually do is start with the API
and then worry about presentation.
So request that you are well that we need.
Of course I copied something else.
So let's go back.
Copy that you are.
This is where we're requesting feel free to make this whatever search term you want whatever state you
live in.
And then we need a callback.
So it's error response and then body and then we add in that if statement.
Technically we don't have to have that.
It's just good to have.
Just so that we're not you know working with broken data and we can handle the error if we need to.
So if there's not an error and response that status code equals 200 if that's the case then we're going
to work with body.
So before we even parse it let's just send the PADI's so does send a body which will be a string at
that point.
And let's see what we get.
Refresh and there we go.
Now when I refresh you can see it starts off.
It's really hard to see because this Chrome extension is kicking in.
But you can see it's this chase on here.
It's actually a string of Jaison.
What we need to do now is turn it into an object.
So if I wanted just to get the first title.
Let's go back here.
I just want King of California or whatever the first title is.
I need to access search and then it's an array.
So I want the zero with element but if I do that here
and save
we get an error which is cannot read properly zero of undefined.
So this is undefined here because body is still a string just like we saw on the last video or two videos
ago.
We need to convert it to an object.
So that's where we get our Jason parce body I'll save it to let's just call it results or.
Results is fine.
Jaisalmer up parch body.
And then we just replace body here with results.
So that gives us the object form of that string that's been parsed and converted then we can access
it just like any other object.
And now we get this single element and then if we just want the title
restart at one more time and we get King of California.
Excellent.
So so pretty straightforward really.
We're not done of course but all that we did was basically take our request code that we had earlier
and just put it inside of a wrap.
So next up we'll be taking the response and actually creating a template that uses it and displays all
of the movie titles in just a simple list.
And it's a speech to a mouse rather than just plain text that we see now.
All right