EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
JavaScript
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Languageschevron-right
JavaScriptchevron-right

Build a Simple Website with React, Axios, and a REST API Built with MongoDB Atlas App Services

Maxime Beugnet4 min read • Published Feb 14, 2022 • Updated Jan 26, 2023
ReactJavaScript
Facebook Icontwitter iconlinkedin icon
Rate this code example
star-empty
star-empty
star-empty
star-empty
star-empty

Introduction

REST APIs and React are both very popular technologies in their respective fields and they are also very interesting to combine in a project.
In this blog post, I will show you how we can use a REST API that I created with the help of MongoDB Atlas App Services to access my data in my MongoDB Atlas cluster and display the result of the query in a website built with React.
At the end of this tutorial, you will have a basic React project capable of retrieving a list of countries through a REST API and displaying them on a web page.
In the next blog post, I will use this project as a starting point. The list of countries will become a filter for a bunch of charts built with MongoDB Charts that I will be able to import into a website using the MongoDB Charts SDK. The final result will be a COVID-19 dashboard which will be easy to filter by country.

Prerequisites

To build this project, you will need a recent version of NodeJS.
Please check that your versions of node, npm, and npx are close to my versions:
In the body of this blog post, I will step through how to build this app. If you don't want to follow along, and you prefer to jump to the final project, you can clone this repository from Github:

Create a React Application

The last command should automatically open a new tab in your favorite browser at the address http://localhost:3000, and you should see the default React spinning logo. Before we continue, we can simplify the project by removing all the files we don't need.
We now have a clean canvas for the next step.

Install Axios

Axios is a promise-based HTTP client for Node.js and the most famous HTTP client, as far as I know, with currently more than 14 million weekly downloads. We will use it to query our REST API and retrieve the list of countries to display.

Checking the REST API

As mentioned in the introduction, the REST API we are going to use in this blog post already exists and was created using MongoDB Atlas App Services so it can scale easily and automatically. This REST API exposes metadata from the COVID-19 Open Data Cluster that we made publicly available. Check out this blog post for all the details.
This is the REST API we will use. You can open it in a new tab in your browser or use the curl command:
The result from accessing this REST API is a JSON document that looks like this:
And it contains the list of countries that I want, which is the list of all the countries that exist in the Johns Hopkins University data set.
Now that we have everything we need to build our React website, let's code!

Building the Website

If you followed all the instructions so far, here is what your working folder should look like:
The current favicon is the React logo. You can totally replace it with the MongoDB one 😎.
Let's now edit the contents of public/index.html. Nothing really fancy compared to the original version that came with the sample application. I just removed what wasn't necessary.
The only thing that we need to notice in this file is the fact that we now have an empty div with an id root which we will use to inject our React content.
Let's create a new file src/index.js:
We are now injecting our React content into our website and the content will be what the new RestExample component will render.
Let's create this new function component in the file src/RestExample.js:
You can now start the project with the command:
There are a few things that are worth explaining here if you are new to React:
  • On line 6, we are creating a state, countries, that is initialised to an empty array and a function setCountries() that can alter the value of that variable. More documentation here.
  • With the help of the useEffect() function (starting at line 8) and the Axios library, we can execute the function once, extract the countries from the response, and set the countries values from our list. More documentation here.
  • Finally, our function component will render the returned content. The countries.map() function iterates over the list of countries and transforms each of the strings into an <li> tag to create an HTML unordered list.

Final Result

The final website should look like this:
Website with React, MongoDB, and Axios
If you got confused at any point, and your app isn't working, just clone the final project:

Summary

In this blog post, you learned how to create a basic React website that uses Axios to retrieve data from a MongoDB Atlas Cluster using a REST API implemented with MongoDB Atlas App Services.
In the next blog post in this series, we'll extend this project so that we can integrate some charts from MongoDB Charts. We'll show COVID-19 data for a country and select which country we want to filter by. Stay tuned!
If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.

Facebook Icontwitter iconlinkedin icon
Rate this code example
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Build a Totally Serverless REST API with MongoDB Atlas


Feb 03, 2023 | 5 min read
Tutorial

How to Build a Healthcare Interoperability Microservice Using FHIR and MongoDB


Oct 17, 2023 | 14 min read
Tutorial

Serverless MEAN Stack Applications with Cloud Run and MongoDB Atlas


Apr 02, 2024 | 8 min read
Tutorial

Add Memory to Your JavaScript RAG Application Using MongoDB and LangChain


Apr 01, 2024 | 9 min read
Technologies Used
Languages
Technologies
Table of Contents