Just like with databases, sometimes servers need to make requests to external APIs to accomplish some goal. There are a variety of reasons to reach out to external services. Some common situations are payment processing, service integrations with other products, webhooks, and so on.
There are a few methods provided by the http
module that facilitate making HTTP requests to external services. One of these methods is the request()
method. The request()
method takes two arguments; it takes a configuration object containing details about the request as well as a callback to handle the response.
const options = { hostname: 'example.com', port: 8080, path: '/projects', method: 'GET', headers: { 'Content-Type': 'application/json' } } const request = http.request(options, res => { // Handle response here })
For convenience, the http
module provides a convenient method for making GET
requests in the form of the get()
method. This method differs from the request()
method in that it automatically sets the method to GET
and calls req.end()
automatically.
The fact that servers can make HTTP requests to other services opens up possibilities for different architecture designs for back-ends. One example of an architecture made possible by this ability is microservice architectures. Microservice architectures divide needs into separate lightweight services that communicate via HTTP over a network. As such, a single application can be comprised of dozens of microservices, which could all be written in different programming languages, but work together by communicating over HTTP.
Instructions
In our HTTP server, we will process incoming GET
requests by invoking a handler function called handleGetRequest()
. Part of this processing will include making a GET
request to the following URL which requires some configuration.
https://static-assets.codecademy.com/Courses/Learn-Node/http/data.json
Let’s begin this configuration by creating a const
variable called options
within the handleGetRequest()
function. Set the variable to be an object containing the properties hostname
, path
, and method
. Using the URL above, set appropriate values for the properties of the options
object.
Note: In the code editor, you may notice that both the http
and https
modules are being used. We use the https
module to request data over HTTPS protocol and use the http
module to create a localhost server to run our node application.
Now that the request has been configured, we can execute the request in the handleGetRequest()
function. To make the request, we will use the .request()
method from the https
module.
Create a const
variable called request
. Set the variable to the https.request()
method with the options
object as the first argument and a callback function as the second argument. Create a callback function using the ES6 arrow function syntax. The callback function should also take a single argument called response
.
Our HTTPS request needs to be set up to be able to receive the data properly. Data from the response will usually be received in chunks and pieced together. As such, we need to watch for these chunks of data and process them. We can do this by listening to the data
event.
In the callback of the .request()
method, create a variable called data
initialized with an empty string. Then, listen for the data
event and add the received data to the data
variable.
Once all of the data has been received from the response, we need to handle that data.
To know when a response is finished, we can listen for the end
event. Still in the callback of the .request()
method, set up a listener for the end
event of the response.
When the end
event is triggered, we can work with the data from the response. In this case, we will act as a proxy and relay the data from the external API to the client that made the request. In the callback of the listener for the end
event, log the data retrieved from the API to the console, then return the data by dispatching a response to the caller.
After the definition of request
but still within handleGetRequest
, be sure to signal the end of the request.
Run your code with node app.js
, then click on the “Check Work” button.