Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
catherineisonline committed Jul 26, 2024
1 parent 9a9f61d commit 57e4eeb
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 79 deletions.
42 changes: 24 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ shortlyBtn.addEventListener("click", (e) => {
});
function fetchBackupUrl(inputValue) {

fetch(`http://localhost:3000/shortener?inputValue=${encodeURIComponent(inputValue)}`).then((response) => response.json())
.then((response) => {
fetch('https://pizza-time-backend.vercel.app/shortener', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ inputValue: inputValue })
}).then((response) => {

if (response.success) {
let shortlyCode = response.data.url;
resultSkeleton = `<div class="result">
if (response.success) {
let shortlyCode = response.data.url;
resultSkeleton = `<div class="result">
<p class="inserted-link">${inputValue}</p>
<hr class="result-block-hr">
<div class="results">
Expand All @@ -58,22 +64,22 @@ function fetchBackupUrl(inputValue) {
</div>
</div>`;

if (sessionStorage.getItem("resultsStorage") !== null) {
resultStorage = [resultSkeleton, sessionStorage.getItem("resultsStorage")]
parentNode.innerHTML = [resultStorage].join('').split(',').join('');
sessionStorage.setItem("resultsStorage", [resultStorage].join('').split(',').join(''));
resetResults.classList.add("active");
}
else {
parentNode.insertAdjacentHTML('afterbegin', resultSkeleton);
if (sessionStorage.getItem("resultsStorage") !== null) {
resultStorage = [resultSkeleton, sessionStorage.getItem("resultsStorage")]
parentNode.innerHTML = [resultStorage].join('').split(',').join('');
sessionStorage.setItem("resultsStorage", [resultStorage].join('').split(',').join(''));
resetResults.classList.add("active");
}
else {
parentNode.insertAdjacentHTML('afterbegin', resultSkeleton);

resultStorage.push(resultSkeleton);
sessionStorage.setItem("resultsStorage", [resultStorage].join('').split(',').join(''));
resetResults.classList.add("active");
}
resultStorage.push(resultSkeleton);
sessionStorage.setItem("resultsStorage", [resultStorage].join('').split(',').join(''));
resetResults.classList.add("active");
}
}

})
})
.catch(error => console.log(`Error with API: ${error.message}`));
}

Expand Down
123 changes: 62 additions & 61 deletions server.mjs
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
import express, { json } from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
import dotenv from 'dotenv'
dotenv.config();
const app = express();
const port = 3000;
const SHORTENER_API_URL = process.env.SHORTENER_API_URL;
const SHORTENER_API_KEY = process.env.SHORTENER_API_KEY;


app.use(cors());
app.use(json());

app.get('/', (req, res) => {
res.send('Server Deployed 🥳');
});

app.get(`/shortener`, async (req, res) => {
const { inputValue } = req.query;
if (!inputValue) {
return res.status(400).json({ success: false, message: 'URL parameter is required' });
}

try {

const apiUrl = SHORTENER_API_URL;
const apiKey = SHORTENER_API_KEY;
const payload = {
url: inputValue,
domain: "tinyurl.com",
};
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
})
if (!response.ok) {
const errorText = await response.text();
console.error(`HTTP error! Status: ${response.status} - ${errorText}`);
throw new Error(`HTTP error! Status: ${response.status}`);
}

const data = await response.json();
if (data.code === 0) {
const shortenedUrl = data.data.tiny_url;
res.json({ success: true, data: { url: shortenedUrl } });
} else {
console.error('Failed to shorten URL:', data);
res.status(500).json({ success: false, message: 'Failed to shorten URL' });
}
} catch (error) {
res.status(500).json({ success: false, message: `Error with external service: ${error.message}` });
}
});

app.listen(port, () => console.log(`Server is running on http://localhost:${port}`));
// import express, { json } from 'express';
// import cors from 'cors';
// import fetch from 'node-fetch';
// import dotenv from 'dotenv'
// dotenv.config();
// const app = express();
// const port = 3000;
// const SHORTENER_API_URL = process.env.SHORTENER_API_URL;
// const SHORTENER_API_KEY = process.env.SHORTENER_API_KEY;
// const allowedOrigins = ['https://catherineisonline.github.io', 'http://localhost:3000', 'http://127.0.0.1:8080/'];


// app.use(cors());
// app.use(json());

// app.get('/', (req, res) => {
// res.send('Server Deployed 🥳');
// });

// app.get(`/shortener`, async (req, res) => {
// const { inputValue } = req.query;
// if (!inputValue) {
// return res.status(400).json({ success: false, message: 'URL parameter is required' });
// }

// try {

// const apiUrl = SHORTENER_API_URL;
// const apiKey = SHORTENER_API_KEY;
// const payload = {
// url: inputValue,
// domain: "tinyurl.com",
// };
// const response = await fetch(apiUrl, {
// method: 'POST',
// headers: {
// 'Accept': 'application/json',
// 'Authorization': `Bearer ${apiKey}`,
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify(payload)
// })
// if (!response.ok) {
// const errorText = await response.text();
// console.error(`HTTP error! Status: ${response.status} - ${errorText}`);
// throw new Error(`HTTP error! Status: ${response.status}`);
// }

// const data = await response.json();
// if (data.code === 0) {
// const shortenedUrl = data.data.tiny_url;
// res.json({ success: true, data: { url: shortenedUrl } });
// } else {
// console.error('Failed to shorten URL:', data);
// res.status(500).json({ success: false, message: 'Failed to shorten URL' });
// }
// } catch (error) {
// res.status(500).json({ success: false, message: `Error with external service: ${error.message}` });
// }
// });

// app.listen(port, () => console.log(`Server is running on http://localhost:${port}`));

0 comments on commit 57e4eeb

Please sign in to comment.