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
axiosto perform aGETrequest 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
- If you are starting a new project, create a directory,
cdinto it, and runnpm init -y. - Install the necessary packages:
expressandaxios.npm install express axios
Task 2: Create the Express Server
- Create a file named
server.js. - 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
- In
server.js, create a newGETroute at the path/api/fun-fact. - This route will be responsible for fetching the data and sending it to the client.
Task 4: Fetch from an External API
- 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. - Inside your
/api/fun-factroute handler, useaxiosto make aGETrequest to this URL. - Remember to use
async/awaitand wrap your API call in atry...catchblock to handle potential errors.
Task 5: Handle and Respond with Data
- If the
axiosrequest 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" } - Your API should not send this entire object to the client. Instead, extract only the
textof the fact. - Send a JSON response back to the client in the following format:
{ "fact": "A standard deck of cards is a calendar..." } - If the request fails (i.e., the
catchblock 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
- Ensure your
server.jsfile is complete and yourpackage.jsonincludes the required dependencies. - Run your server with
node server.js. - Test your API by visiting
http://localhost:3000/api/fun-factin your browser. You should see a new random fact in the specified JSON format each time you refresh. - Once complete, submit a link to your GitHub repository containing the project files.
Reflection Questions
After completing this activity, consider the following:
- 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?
- In the
catchblock, why is it better to send a generic error message to the client instead of the actual error object fromaxios? - 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)?