Now we are actually going to go and make a project where we are going to utilize the API that we just
found.
So I'm going to make a directory.
I will call it Nordstrom scraper.
And go inside the directory CD Nordstrom scraper and then initialize npm.
NPM init.
Dash.
Dash js.
Once we initialize the project or the NPM, I'm going to open the folder inside of Visual Studio code.
Now inside Visual Studio code.
I'm going to make a folder here and we'll call it the back end.
And we're going to put an index.js file in here.
Then we're going to add some packages.
Now we're going to add our trusty old request and request promise.
Let me just enhance that.
So you can see the text.
So request and need to add yarn, add yarn.
Add request Package.
Request Promise.
And we also need to add the express package.
So express is a way to make rest APIs inside of NodeJS.
So we add those.
And now we go ahead and write our back end.
So the first thing we have is we have express.
So we say require express.
And then we make what's called we call it app where we initialize express.
And what we do then is we say app, listen, what port we want to run on.
So we say we run it on port 4000.
Then we have a function that runs once the NodeJS API is running.
So we write console.log server running on port 4000 just like that.
And then I'm going to run the API in the terminal here.
So I'll do CD back end to the folder here containing the index.js and then I'll run Node man Index.js.
If you don't have Node Man installed already, go ahead and install it using NPM install dash g a node
one.
So the dash G makes it a global install, which means that you can use it anywhere inside of your terminal.
And Norman is great because when we make changes inside Index.js.
It's going to automatically reload the changes.
So if we change this to.
Something like that, it's going to automatically start the script again so you don't have to type node
dot node index.js all the time.
You can just have this Node one running instead.
So now we have our NodeJS Express server running, but we're actually not able to use it so much because
we don't have any endpoints we're listening to and that's what we're going to do now.
So let's say app dot get and we'll say slash Nordstrom and then we can have a request.
So this is the request parameters we get in.
I'll get into that longer and next time then response and then next.
And so this one is basically how you're calling the API.
What parameters are you sending as you're calling the API?
We'll get into that.
Next rest is what you use to send a response back from the API to the user.
And next is something you run.
You can run next like middleware stuff.
We're not going to be using that in this section.
Now, I want to make a little disclaimer here and say you could make a course about this, about NodeJS
Express.
And there's a lot of courses out there about NodeJS Express.
I'm not going to go into detail and teach you how everything there is to know about NodeJS Express.
This is just to get you up and running, having a scraping API running.
And so if you want to know more about NodeJS Express, there's a lot of great courses out there you
can take.
So please don't be discouraged if some of this just doesn't make sense to you.
It's a basically a whole course in itself to learn, but this is going to get you up and running, running
a web scraping API.
So if you follow me step by step, don't worry, we are going to get through this and you will have
a web scraping API in no time.
Next up is we want to make a call to this API that we just discovered and.
And so we had this.
We had it inside of Postman.
Let me see if I can get postman up and running here.
So here we have the URL that we came to that we found out was working with the parameters here, the
query parameters and the headers.
So let's just copy the URL here and.
You know what?
Let's first try and just get this running.
Let's try and say a hello world just so you can see how it is running.
So now it is running on this endpoint where we do a get request on this endpoint here, Nordstrom and
it's going to return a Hello world.
So if we do inside of Postman if you type in.
A Http and you type in localhost and you say 4000 and you say Nordstrom.
Then.
It's going to say Hello world.
And that's just for pure text.
It's not Json, but basically you now have an API running on NodeJS on your own local computer, on
Port 4000 on the Norstrom endpoint, but we obviously want to return the Nordstrom API results instead
so we can use them inside of our React client.
Let me show you how we do this.
So we have the URL.
I copied that from the postman request we did before in the previous section.
And so let me see.
So I copied that in here and I say I copied into a variable const URL.
And now we're going to try and make a request inside of our API.
So remember in the previous lecture we imported request also.
Well, we going to start using that now.
So let's import request here.
Require.
My quest.
Promise.
What I want to show you now is remember in the Postman API, we found out we needed these two headers.
We needed the authorization header with the API key and we also needed the API version in the header.
So we need to have those inside the request.
Import as well.
We're just going to set it by default to have that in here.
So we say dot defaults.
And then we put in our headers.
We can put that in here.
And then remake one hitter.
That's called authorization and we paste in the API key we have here that we made inside of our postman.
Paste it in here.
Then the newer API version.
And we set that to one.
So now we have the same headers that we had inside of Postman that we found out was working to get to
the the results inside of the API.
Now all that is left now is to do a little request and return it.
So let's see.
So we have this is going to return the Json so we say request.
Actually let me use a weight also because it's async request.
So we want to use await.
So request dot get.
Earl and.
Then we say rest.
So rest set, Jason.
So rest is the object you can use inside to send Jason.
And now, since we're using Await inside here, I'm going to have async in front of the function here.
Okay, now let's see what is going on when we try to make a request to the Nordstrom endpoint.
Now inside of our API.
So before it was just Hello world.
Let's see what it says now.
I'd still say subtle world.
Okay.
That's because we're doing a recent Hello world up here.
So it's not we're never getting down to this part, so we'll just remove the recent hello world here
and let's try again.
And there we go.
So we return.
Oops.
We return.
We get lots of Actually, it is Jason, but it's like formatted like text in here.
So we need to you can see the headers of the response here is saying content-type text HTML.
So we need to also set the header to be a content type of Json.
So that's pretty easy to do.
We just do rest dot header.
Oh no restart set hitter and we say content type.
I'm passing application slash Json and that is going to make Postman and other applications recognize
that this is Jason that we are receiving.
Now let's try and do it again.
And now it gets a prettified it gets formatted as Json.
So here we go.
So this is the exact same response that we would get inside of the the result we get from the API here.
It's exactly the same.
We're just passing it through our own API instead so we can use it in our front end applications like
our React app.
We're going to make.
Okay.
So.
Let's see.
In the next section we are going to pass in query parameters also so we can decide how many results
we want.
This top parameter here and we are also going to pass in the keyword parameter.