React frontend application that utilizes various NASA APIs to display astronomy-related data, including Mars Rover Photos, Astronomy Picture of the Day, and Earth imagery APIs. Users can explore daily or historical data and interact with the application to view dynamic content.
You can access the hosted application by following this link:
NASA API Explorer provides users with a user-friendly interface to explore data from NASA's APIs. It offers a seamless experience for viewing Mars Rover Photos, Astronomy Picture of the Day, and Earth imagery. Users can choose to view daily updates or search for historical data.
- Frontend: React + Vite
- Backend: Express Js, Node JS
- Database: MongoDB
- Language: JavaScript
- CSS Framework: Tailwind CSS
- Session Management: JSON Web Token
- Version Control: GitHub
- Hosting Platform: Vercel
To run this project locally, follow these steps:
-
Create a New Folder Open it
-
Press Alt + D to select the address bar.
-
Type cmd and press Enter. This will open a command prompt window with the current folder as its location. (this method will open a command prompt window with the directory set to the folder you specified, allowing you to run commands directly in that folder)
-
Clone the repository to your local machine using Git:
git clone <github_repo_link>
- To Run Backend and Frontend of the application follow below instructions.
- Navigate to the project directory
cd backend
- Install the project dependencies using npm.
npm i
-
Set up environment variables in a .env file (Place .env File On Backend Folder) Add environment variables ex: DataBase Link , SALT, JWTPRIVATEKEY
-
run backend
npm start
-
Open another terminal.
-
Navigate to the project directory.
cd frontend
- Install the project dependencies using npm.
npm i
-
Set up environment variables in a .env file (Place .env File On Frontend Folder). ex :VITE_NASA_API_KEY (your nasa api key) For How to generate NASA API Key Look API Integration Section.
-
run frontend
npm run dev
deployed using Vercel for seamless hosting.
-
Why vercel,
-
Real-time Updates: With Vercel, our application updates automatically in real-time whenever push commits to GitHub.
-
Free Hosting: enjoy free hosting on Vercel's generous free tier, saving costs while ensuring our application is always accessible.
-
Simply follow these steps to host your react - node app:
- Sign up for an account on Vercel.(Login with github accout)
- Install Vercel to Your Repository (selcet the repo you want to Host)
- Import Repository
- Configure Project In Root Directory first select backend and Add Environment Variables Like MongoDB url Secret KEY ect. And Deploy .
- Also For frontend Import to your currunt project cahnge name use frontend or what you like then Root Directory select frontend Add Environment Variables Like if you need . Then Deploy.
In Your Frontend, where you Connect with backend. use Backend Domain addres you deplployed insted of your Localhost. ex: like this
https://web-app-server.vercel.app/api/v1/
- In package.json change nodemon to node
"scripts": {
"start": "node server.js"
},
- Create vercel.json and coppy this.
{
"version": 2,
"builds": [
{
"src": "server.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/server.js"
}
]
}
- In Server.js
app.use(cors({
origin: 'https://yourwebappname.vercel.app', // Include your Frontend Domain address
methods: 'GET,POST', // Allow all request methods
credentials: true // Allow credentials
}));
- Link to nasa open api
- signup and get key via email
- use .env to keep api key secured no one can see.
NASA Astronomy Picture of the Day (APOD) API to fetch daily images and related information about space and astronomy. The APOD API provides a rich source of fascinating images along with their descriptions, which utilize to enhance the user experience of our application.
fetch data from the NASA (National Aeronautics and Space Administration) API to retrieve the Astronomy Picture of the Day (APOD) and related information. Here's an overview of how fetch and cache data from the NASA API:
use JavaScript's built-in fetch
function to make HTTP requests to the NASA APOD API endpoint. Here's a breakdown of the steps involved:
-
API Endpoint: construct the API request URL using the base URL
https://api.nasa.gov/planetary/apod
and include our NASA API key as a query parameter (api_key
). The API key is securely stored in our application environment. -
Local Data Cache: Before making a request to the API, check if the data for the current day is available in the local storage cache. generate a unique cache key based on the current date and prefix it with 'NASA-' (e.g.,
NASA-Sun Apr 25 2024
). If data for the current date is found in the cache, retrieve and use it, skipping the API request. -
Fetching Data: If the data for the current day is not available in the cache, proceed to make a request to the NASA API using the constructed URL. use
fetch
to perform an asynchronous HTTP GET request to the API endpoint. -
Processing Response: Upon receiving a response from the API, extract the JSON data using the
json
method. The fetched APOD data typically includes information such as the image URL, title, date, and explanation. -
Caching Data: Once receive the data from the API, store it in the local storage cache using the unique cache key generated earlier. This cached data allows us to avoid making redundant API requests for the same day in future application sessions.
-
Error Handling: handle any errors that occur during the API request or data processing. If an error occurs, log the error message to the console for debugging purposes.
During the integration of the APOD API, encountered several challenges:
-
API Key Management: Initially, managing the API key securely posed a challenge. needed a reliable method to securely store and retrieve the API key without exposing it in our codebase.
-
Caching Data: Another challenge was efficiently caching fetched data to minimize unnecessary API calls. aimed to store the retrieved APOD data locally to reduce network requests and improve application performance.
To address the challenges faced during API integration, implemented the following solutions:
-
Secure API Key Handling: leveraged environment variables and configuration files to securely manage the API key. By storing the API key in an environment variable, ensured that it remained private and inaccessible to unauthorized users.
-
Data Caching: To optimize performance and minimize API calls, implemented a caching mechanism using browser local storage. Upon fetching data from the APOD API, stored it locally, associating each entry with the current date. Subsequent requests for the same date retrieved the data from the local cache instead of making additional API calls, reducing network overhead.
Integrating the APOD API into our project proved to be a valuable addition, enriching the user experience with captivating images and informative content about space and astronomy. By overcoming challenges such as API key management and data caching, ensured the smooth functioning and optimal performance of our application.
NASA EPIC API to retrieve Earth imagery captured by the DSCOVR satellite. The API provides access to both natural and enhanced color images taken on specific dates. By integrating this API, users can explore stunning images of our planet taken from space.
-
API Key Handling: One challenge was securely handling the NASA API key. used environment variables through Vite (
import.meta.env
) to store the key securely and access it within the application. -
Date Formatting: The API requires dates to be in a specific format. faced challenges in formatting dates correctly to match the API's requirements. However, resolved this by converting JavaScript Date objects to the required ISO format using
toISOString()
. -
Data Handling: The API responses contain various data fields, and handling them dynamically posed a challenge. addressed this by parsing the response data and displaying relevant information based on user selections.
-
Image Format: Initially, images were not fetched in PNG format, causing issues with rendering. To resolve this, modified the image URL to explicitly request the PNG format from the API.
-
API Key Handling: securely stored the NASA API key in a Vite environment variable (
VITE_NASA_API_KEY
). This approach keeps the key out of version control and ensures security. -
Date Formatting: To correctly format dates for API requests, used JavaScript's
toISOString()
method to convert Date objects into the required format. This ensured compatibility with the API's date parameters. -
Data Handling: parsed the API response data and displayed relevant information dynamically based on user selections. This involved conditional rendering of components and fields, ensuring a seamless user experience.
-
Image Format: updated the image URL to explicitly request the PNG format from the API. By appending
.png
toimageData.image
, ensured that images are fetched in the desired PNG format, resolving rendering issues.
The Mars Rover Photos Viewer component in our application utilizes the Mars Rover Photos API provided by NASA. This API allows users to retrieve photos taken by NASA's Mars rovers. Users can specify the Mars sol (solar day on Mars), the rover's camera, and pagination options to explore various photographs captured on the Martian surface.
The Mars Rover Photos API offers access to a comprehensive collection of images from different cameras mounted on three rovers — Curiosity, Opportunity, and Spirit — based on several parameters:
sol
: Mars solar day of the rover mission.camera
: Specific camera on the rover.page
: Pagination parameter for the response.
The Mars
component is implemented as a React functional component using hooks such as useState
for managing state (e.g., selected sol, camera, and page) and useEffect
for side effects to fetch data based on user input.
Challenge: Initially, frequent API calls during development quickly led to hitting the rate limit imposed by NASA's API. Solution: implemented conditional fetching to minimize unnecessary requests and introduced a simple caching mechanism to store previously fetched data. Additionally, added error handling to gracefully inform users when the rate limit has been exceeded.
Challenge: The API's response structure varied significantly based on the query parameters, sometimes leading to errors in rendering when data was absent. Solution: added robust error handling and data structure checks before attempting to render the photos. This ensured that the application could handle empty or unexpected responses without crashing.
- Tests with Jest and SuperTest
- setup
npm i jest supertest cross-env
- Package.json add test script
"scripts": {
"test": "cross-env NODE_ENV=test jest --testTimeout=5000",
}
- create test folder in backend mkdir tests
- create test file for each test ex users.test.js and write test case for each
- run test
npm test
- Using Jest and React Testing Library and Vitest.
- Set Up the Testing environmet in frontend.
- install Jest and React Testing Library
npm install vitest jsdom @testing-library/jest-dom @testing-library/react @testing-library/user-event -D
- package.json update
"scripts": {
"test": "vitest"
}
3.mkdir tests (create a test folder in root frontend) setup.js
import { afterEach, vi } from "vitest";
import { cleanup } from "@testing-library/react";
import "@testing-library/jest-dom/vitest";
afterEach(() => {
cleanup();
vi.clearAllMocks();
});
- update vite.config.js
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
test: {
globals: true,
environment: "jsdom",
setupFiles : "./tests/setup.js"
},
})
- implement test files create ex:login.test.jsx
- run test
npm test
or
npm run test:ui
Test Results :