Learn

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.

microservice diagram

Instructions

1.

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.

2.

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 http module.

Create a const variable called request. Set the variable to the http.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.

3.

Our HTTP 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.

4.

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.

5.

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, return the data retrieved from the API by dispatching a response to the caller.

Run your code with node app.js, then click on the “Check Work” button.

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?