Skip to content

Commit

Permalink
SpaceX API
Browse files Browse the repository at this point in the history
  • Loading branch information
revanth1718 authored Aug 9, 2024
1 parent 72bc4cf commit 474acb2
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 0 deletions.
23 changes: 23 additions & 0 deletions New_APIs/SpaceX API/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>SpaceX Explorer</title>
</head>
<body>
<header>
<h1>SpaceX Explorer</h1>
</header>
<main>
<section>
<button id="fetchLaunches">Latest Launches</button>
<button id="fetchRockets">Rockets</button>
<button id="fetchMissions">Missions</button>
<div id="dataDisplay"></div>
</section>
</main>
<script src="index.js"></script>
</body>
</html>
41 changes: 41 additions & 0 deletions New_APIs/SpaceX API/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
document.getElementById('fetchLaunches').addEventListener('click', async () => {
await fetchData('/launches');
});

document.getElementById('fetchRockets').addEventListener('click', async () => {
await fetchData('/rockets');
});

document.getElementById('fetchMissions').addEventListener('click', async () => {
await fetchData('/missions');
});

async function fetchData(endpoint) {
try {
const response = await fetch(endpoint);
const data = await response.json();
displayData(data);
} catch (error) {
console.error('Error fetching data:', error);
document.getElementById('dataDisplay').textContent = 'Failed to fetch data.';
}
}

function displayData(data) {
const displayDiv = document.getElementById('dataDisplay');
displayDiv.innerHTML = '';

if (Array.isArray(data)) {
data.forEach(item => {
const itemDiv = document.createElement('div');
itemDiv.classList.add('item');
itemDiv.innerHTML = `
<h3>${item.name || item.mission_name || item.rocket_name}</h3>
<p>${item.details || item.description || item.mission_id}</p>
`;
displayDiv.appendChild(itemDiv);
});
} else {
displayDiv.textContent = 'No data available.';
}
}
19 changes: 19 additions & 0 deletions New_APIs/SpaceX API/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions New_APIs/SpaceX API/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "spacex-explorer",
"version": "1.0.0",
"description": "A simple app to explore SpaceX data using the SpaceX API",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"node-fetch": "^2.6.1"
}
}

44 changes: 44 additions & 0 deletions New_APIs/SpaceX API/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require('express');
const fetch = require('node-fetch');

const app = express();
app.use(express.static('public'));

const SPACEX_API_BASE_URL = 'https://api.spacexdata.com/v4';

app.get('/launches', async (req, res) => {
try {
const response = await fetch(`${SPACEX_API_BASE_URL}/launches/latest`);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching launches:', error);
res.status(500).json({ error: 'Failed to fetch launches' });
}
});

app.get('/rockets', async (req, res) => {
try {
const response = await fetch(`${SPACEX_API_BASE_URL}/rockets`);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching rockets:', error);
res.status(500).json({ error: 'Failed to fetch rockets' });
}
});

app.get('/missions', async (req, res) => {
try {
const response = await fetch(`${SPACEX_API_BASE_URL}/missions`);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error fetching missions:', error);
res.status(500).json({ error: 'Failed to fetch missions' });
}
});

app.listen(3000, () => {
console.log('Server is running on port 3000');
});
70 changes: 70 additions & 0 deletions New_APIs/SpaceX API/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
body {
font-family: 'Arial', sans-serif;
background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
margin: 0;
}

header {
text-align: center;
margin-bottom: 20px;
}

header h1 {
font-size: 3em;
color: #fff;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.6);
}

main {
background: rgba(0, 0, 0, 0.6);
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
width: 80%;
max-width: 800px;
text-align: center;
}

button {
padding: 10px 20px;
margin: 10px;
font-size: 1.2em;
color: #fff;
background-color: #333;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}

button:hover {
background-color: #555;
}

#dataDisplay {
margin-top: 20px;
text-align: left;
}

.item {
background: rgba(255, 255, 255, 0.1);
padding: 15px;
margin: 10px 0;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.item h3 {
margin-top: 0;
color: #fdbb2d;
}

.item p {
margin: 5px 0 0;
}

0 comments on commit 474acb2

Please sign in to comment.