Skip to Content
Lab 2

Server to Server Communication

Scenario

The owner of “The Daily Grind” coffee shop was so impressed with your first server that they have a new request. They want to add a “Fun Fact of the Day” to a digital screen in their shop. To do this, they need an API endpoint on their server that fetches a random fun fact from an external public API and provides it in a simple format.


Learning Objectives

By the end of this activity, you will have demonstrated your ability to:

  • Create an Express server that communicates with an external API.
  • Use axios to perform a GET request to a public API.
  • Handle the response from an external API and serve the relevant data to a client.
  • Implement basic error handling for API requests.

Instructions

Task 1: Project Setup

  1. If you are starting a new project, create a directory, cd into it, and run npm init -y.
  2. Install the necessary packages: express and axios.
    npm install express axios

Task 2: Create the Express Server

  1. Create a file named server.js.
  2. Inside this file, set up a basic Express server that listens on a port (e.g., 3000) and logs a message to the console on startup.

Task 3: Create the API Route

  1. In server.js, create a new GET route at the path /api/fun-fact.
  2. This route will be responsible for fetching the data and sending it to the client.

Task 4: Fetch from an External API

  1. For this lab, we will use the Useless Facts API, which provides random facts and requires no API key. The endpoint for a random fact is https://uselessfacts.jsph.pl/api/v2/facts/random.
  2. Inside your /api/fun-fact route handler, use axios to make a GET request to this URL.
  3. Remember to use async/await and wrap your API call in a try...catch block to handle potential errors.

Task 5: Handle and Respond with Data

  1. If the axios request is successful, the response data from the Useless Facts API will look something like this:
    { "id": "d046f554-9430-4113-9528-56455147814b", "text": "A standard deck of cards is a calendar...", "source": "djtech.net", "source_url": "http://www.djtech.net/humor/useless_facts.htm", "language": "en", "permalink": "https://uselessfacts.jsph.pl/api/v2/facts/d046f554-9430-4113-9528-56455147814b" }
  2. Your API should not send this entire object to the client. Instead, extract only the text of the fact.
  3. Send a JSON response back to the client in the following format:
    { "fact": "A standard deck of cards is a calendar..." }
  4. If the request fails (i.e., the catch block is executed), send an appropriate error response, such as a 500 status code and a JSON object like { "error": "Could not fetch fun fact" }.

Submission Instructions

  1. Ensure your server.js file is complete and your package.json includes the required dependencies.
  2. Run your server with node server.js.
  3. Test your API by visiting http://localhost:3000/api/fun-fact in your browser. You should see a new random fact in the specified JSON format each time you refresh.
  4. Once complete, submit a link to your GitHub repository containing the project files.

Reflection Questions

After completing this activity, consider the following:

  1. Why was it important to re-format the data from the Useless Facts API before sending it to your own client? What are the benefits of an API providing a clean, minimal response?
  2. In the catch block, why is it better to send a generic error message to the client instead of the actual error object from axios?
  3. How might you modify this application to get a fact in a different language if the external API supported it (e.g., with a query parameter like ?language=de)?

Additional Resources