In this video as promised earlier in the section you're going to learn how to make HDTV requests to
a given U.R.L. without using the request NPM library.
Request is just an NPM library and NPM libraries can't do anything that you could do with node anyways.
Typically they just make it easier to do.
And we saw that with yards and request.
You don't need those to work with command line arguments or to make HDTV requests but it sure does make
the process a whole lot easier.
Now in this video we're going to explore how we can make requests using the modules that node provides.
This is a completely optional video as we're not going to integrate what we learn here into the weather
application.
Instead we're going to just explore it over in the playground directory so we can get a better feel
for what exactly the request module is doing to do this.
Let's make a brand new file.
I'll call this six hyphen raw hyphen HDTV dot J as and in here we're going to essentially recreate one
of our TTP requests firing off the request to the server getting and passing the response and doing
something meaningful with it to get this done.
There are two core modules that we're going to explore we can find those by cracking open the node j
asked documentation in the browser I'm going to close the map box and dark sky API tabs since we're
done with those and I'm going to open up node j s dot org to explore the docs.
Once we're here we're gonna go to the documentation page and we're gonna go to the docs for our version.
Now here we have the table of contents which we've used before.
For example we explored the file system module earlier in the class in this video.
We're gonna take a peek at HDTV and H TTP s.
Now already we can see a bit of an annoyance.
There are separate modules depending on the protocol we're using with request the NPM library.
That's not necessary in abstracts all of that behind the scenes allowing us to easily switch out the
protocol we're using without needing to load in a completely separate library.
Now if we crack both open which I will in a new tab we can take a quick moment to explore them before
using them in our code.
Now both of these modules can be used for both pieces to the puzzle.
You can use these libraries to create a new server and we'll talk about server creation a bit later
in the class and you can also use these modules to make requests to an existing server and that's what
we're going to focus on in this video.
As we scroll down through the table of contents for each near the bottom we're gonna see a couple of
methods and the method we're looking for is request now.
Right here we can use request with options or a U.R.L. with optional options.
Either way it gets the job done and we're gonna see the exact same thing for the H TTP s module if I
scroll down we have those two ways.
That request can be called so we use the TTP module for standard requests and we use H TTP as if we're
making a request to a secure server.
Now in this case both of our API eyes do use the H TTP s protocol.
So that's what we're gonna go ahead and use as well.
Now since it is a core module as a reminder there is no need to install it.
We can simply load it in const H TTP S equals we're going to require the H TTP s core module.
And once again the name I'm choosing to provide here comes from the documentation.
If we scroll down to where we get to some examples you're gonna see that when they load it in.
They use the H TTP s variable name as a standard.
Now it's time to actually use the request function to fire off a request.
In this case let's go ahead and get the forecast for a location.
Though you could also use the same technique to geo code or interact with any other API.
The first thing we need is a U R L to make the request too.
So I'm gonna grab the forecast you are.
All we had in forecast dot J S I'm gonna take the u are all variable in its entirety with the contents
as well.
I'm gonna copy the entire line and bring it over to our new file and paste it in.
Now we are referencing variables which no longer exist so we can remove all of those.
I'm gonna remove the latitude the comma and the longitude and others replace that with some static inline
values.
For example I can used forty comma minus seventy five which is pretty close to where I am.
Not exact but it's good enough for this example.
This is Andrew with a quick reminder that you should be using the new weather stack U.R.L. that you
already had in place.
So up top I have the old dark sky U.R.L. which I just added to the file down below.
We have the new weather stack U.R.L. which is the one you'll end up using.
Now right here I did remove the references to the latitude and longitude variables.
I have set query equal to forty five comma minus seventy five.
So this is the U.R.L. you'll end up using.
And remember that later on when we explore the response you'll end up seeing the response from weather
stack which we've already looked at where we have the current property with our weather data.
All right let's jump back in to the less it down below.
It's now time to fire off the request and we start that process with H TTP s dot request.
This is a function so we're gonna call it as such and we pass to it to arguments.
The first is are you are L which we have access to the.
That you are all variable.
And the second is a callback function and this callback function gets called.
With the response.
This is one last quick update for the lesson.
Remember with whether stack we're using an H TTP U.R.L. since H TTP yes is a paid feature.
So instead of using the TTP s core module like we're doing up above we'll be using the TTP core module
like we're doing down below.
So down below you can see that for the new weather stack code I've called the constant on line 1 h TTP
and I am requiring the TTP module down there online for you'll also see that I'm calling H TTP dot request
instead of h TTP s dot request.
So these are the only changes we need to make.
Just change those three instances from H TTP s over to H TTP and you'll be good to go no other changes
required throughout the rest of the lesson.
All right let's jump back in.
Now the callback we use here is going to be very different from the callback we're used to.
Like we had in the forecast or geo code file our core node modules typically operate at a lower level.
Well NPM modules like request abstract away a lot of that complexity.
Since we are using the core node module here we're gonna have to setup things that you might not think
should be necessary.
For example in this callback we don't have access to the complete response body.
Instead we can go ahead and grab the individual chunks that come through because HDTV data could be
streamed in multiple parts.
So right here what does that mean for us.
It means we have to listen for these individual chunks to come in and we also have to listen for when
all chunks have arrived and the request is done right here.
We can start this process by using response dot on response dot on is a function and it allows us to
register a handler.
Now there are a few different events we can register callback functions for one of them is data and
we provide the event name as the first argument represented as a string.
Now from here we can also provide the callback and that's going to fire when new data comes in and we
get access to that data.
The first and only argument commonly called Chuck so this is a chunk of the response it might be the
entire thing or it might not depending on exactly how the server has been set up.
Now the other thing we're going to need to do in here is figure out when we're done and we can do that
using another call to response dot on.
Here we are waiting for the end event when things are over.
This callback function is going to run and it doesn't get any arguments.
Instead by running we just know we're done.
So now it's time to put these two together to figure out how we can get the entire response body pass
it from Jason to a javascript object and actually use it.
So as I mentioned this callback is going to fire when data comes in.
Now this could happen one time or it could happen multiple times.
Either way we need to take this chunk.
We need to add it somewhere where we can store it until we have all of them then we can pass it as Jason
to up above.
Let's get that done by creating a new variable.
Let data equal an empty string.
So here I'm using left instead of const since I am going to reassign the value over time changing the
string.
Now inside of this callback we're gonna have access to that new chunk.
Let's go ahead and see what the chunk looks like.
By console logging it.
So console dot log chunk.
Now you'll notice that the program in its current state is actually never gonna run that code.
Let's go ahead and prove it.
See dot dot forward slash playground to get into that folder I'll use clear to clear the terminal output
and I'll run our script.
That's these six raw HDTV J.S. file.
Now when I run this things are just gonna Hey it's not going to print our message and it's not going
to bring us back to the terminal for us to run another command.
Instead it's stuck it's stuck because we don't have a complete request.
There's another method we need to use to say we're actually ready to send this off to do that we have
to make a change to our program what we get back from the request method is what you could refer to
as the request itself so constant request can store this variable the variable that comes back as the
return value from request now on here there's a method we can use to kick things off that is request
dot.
And so right here are just another example of how the low level API might not give us the exact tools
we were hoping to it can get a bit confusing even with a simple example there's a lot going on here
with very little progress.
Now we can take things back to the terminal and rerun things by calling and it's going to see that we're
done setting up our request and it will actually fire it off if I do this what do I see.
I can see that right here we have a few different console dot log calls printing various buffers three
of them to the screen.
So these chunk data that comes back is indeed a buffer.
We want a string.
So we're going to go ahead and convert the buffer to the string using the two string method as we've
done earlier in the class and we're going to add the data on to the data variable.
So data is going to equal the old value for data plus the new chunk.
So chunk dot right here to string to convert the buffer to a string.
Now that we have this in place we're gonna have access to the entire value the entire body response
in the data variable down below in end we can go ahead and access it console dot log data.
Now the data callback ran multiple times once for each chunk and is going to run a single time once
things are done down below in the terminal.
I'm going to rerun the program again.
And what do we get.
We get one thing printing to the screen.
It has a very long string.
This is our Jason data.
So from here we could go ahead and pass it to get an object that we could actually pull properties off.
So let's go ahead and get that done.
Const I'll call this something like response body or a body equals.
I will use Jason pass to pass a Jason string which I have stored in the data variable.
Then I'll just print body console dot log body.
Now if I save things and rerun the program from the terminal.
Once again we're gonna see that instead of getting the long string we get our past object ivory around
the program and right here our object prints and we could use this to grab the current temperature or
grab the forecast for the next minutes days or hours.
So this is what it takes to make a request with the core H TTP and H TTP s module and we're not even
really done yet because we don't have any error handling set up.
Let's go ahead and wrap the video up by seeing how you could get that done just up above between where
we fire off their request and use end to actually send it.
We're gonna use request dot on to set up a another listener.
Now the event we're listening for here is error.
This is going to get fired when an error occurs and our callback function which we provide is going
to allow us to do something with that error.
Now the error is the first and only argument and so we can go ahead and name it.
And from here we could do whatever we needed to do with it.
Now in our other files we used a bit of error handling to print one of two messages.
In this case let's go ahead and just dump it to make sure this is even running.
So console dot log in error then as the second argument will actually dump it to the console.
Now with this in place we can go ahead and run the program generating an error to do that.
We can do something simple like turn off our network connectivity.
I'm going to disable Wi-Fi on my machine.
Then down below I'm going to rerun the program and what do I get.
I get the error message looking exactly like what we had much earlier in the class when we initially
explored the error in request.
So now that we have this in place we have a very basic HDTV s request with the core node module.
As you can see this does provide you everything you need but at a much lower level than you probably
expected.
That's why in this class and in the real world people are making requests with those core modules instead
they're using libraries like request access shows or others to make the request process much easier.
Here are callback gets fired a single time when things are ready we either have the error or we have
the response and the body is already sitting there.
We don't have to do anything with it like concatenate together the individual chunks to get everything
we need.
Now you might be thinking why does it no just change how those core modules work to be easier to use
and look a little bit more like a library such as a request.
And that's missing the point.
The core node modules are supposed to provide those low level implementations and Node comes bundled
with NPM because you're supposed to be using NPM modules when you're building out your applications.
That's where we're gonna stop for this bonus video.
We took a dive into exactly how we could use the core node modules to make requests and we've learned
a little bit more about why NPM libraries like request are so valuable to the node ecosystem.
That is it for this video and that is it for this section in this section.
We learned all about a synchronous node j s as we built out our little weather application.
I'm excited to get to the next section.
The section introduction is coming up next.
So let's jump right in.