Skip to content

Commit

Permalink
API removed
Browse files Browse the repository at this point in the history
  • Loading branch information
BrajRaj89 committed Jul 11, 2024
1 parent 109775b commit bd1efb1
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions public/Word_dictionary/app.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
document.addEventListener('DOMContentLoaded', () => {
const input = document.querySelector('#input');
const searchBtn = document.querySelector('#search');
const apiUrl = 'https://www.example.com/api/word-definition'; // Placeholder for the API URL
const notFound = document.querySelector('.not__found');
const defBox = document.querySelector('.def');
const audioBox = document.querySelector('.audio');
const loading = document.querySelector('.loading');
const wordBox = document.querySelector('.words_and_meaning');
let oldLength = 0;

function myFunction() {
async function getApiUrl() {
const response = await fetch('/api/config');
const config = await response.json();
return config.apiUrl;
}

async function myFunction() {
const str = input.value;
if (oldLength !== str.length) {
oldLength = str.length;
const strToSearch = str.split(' ').pop();
getData(strToSearch);
const apiUrl = await getApiUrl();
getData(strToSearch, apiUrl);
}
}

Expand All @@ -40,7 +46,7 @@ document.addEventListener('DOMContentLoaded', () => {
console.log('Speech Recognition API not supported in this browser.');
}

searchBtn.addEventListener('click', e => {
searchBtn.addEventListener('click', async e => {
e.preventDefault();
clearData();
const word = input.value.trim();
Expand All @@ -49,7 +55,8 @@ document.addEventListener('DOMContentLoaded', () => {
return;
}
const wordToSearch = word.split(' ').pop();
getData(wordToSearch);
const apiUrl = await getApiUrl();
getData(wordToSearch, apiUrl);
});

function clearData() {
Expand All @@ -58,7 +65,7 @@ document.addEventListener('DOMContentLoaded', () => {
defBox.innerText = '';
}

async function getData(word) {
async function getData(word, apiUrl) {
loading.style.display = 'block';
try {
const response = await fetch(`${apiUrl}?word=${word}`);
Expand Down Expand Up @@ -113,3 +120,14 @@ document.addEventListener('DOMContentLoaded', () => {
wordBox.appendChild(br);
}
});
const express = require('express');
const app = express();
const port = 3000;

app.get('/api/config', (req, res) => {
res.json({ apiUrl: process.env.API_URL });
});

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

0 comments on commit bd1efb1

Please sign in to comment.