Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
emrebilge committed Sep 17, 2023
1 parent 2ee59d9 commit 57ea05e
Show file tree
Hide file tree
Showing 729 changed files with 197,722 additions and 61,448 deletions.
77 changes: 52 additions & 25 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,70 @@
const express = require('express');
const cors = require('cores');
const cors = require('cors');
const axios = require('axios');
const app = express();
const PORT = 3000;

// app.use(cors()); // this is for security, eventually we need it so that
// see for yourself what happens when you get to the front end stage^
app.use(cors());

const baseURL = 'https://random-word-api.herokuapp.com/word';

let score = 0;
let guesses = 0;
let correct = 0;
let words = [];

// Function to fetch a random word from the API
async function fetchRandomWord() {
try {
const response = await axios.get(`${baseURL}`);
return response.data; // Extract the first word from the API response array
} catch (error) {
throw error; // Handle errors as needed
}
}

app.get('/', (req, res) => {
res.send('hello world!');
});
// Function to scramble a word (replace this with your actual scramble logic)
// Function to scramble a word (replace this with your actual scramble logic)
function scrambleWord(word) {
// Implement your word scrambling logic here
// For example, you can shuffle the characters in the word
word = word[0];
const shuffledWord = word.split('').sort(() => Math.random() - 0.5).join('');
return shuffledWord;
}


app.get(`/score`, (req, res) => {
res.send(`${score}`);
app.get('/', (req, res) => {
res.send('Hello, World!');
});

app.get('/get_word', async (req, res) => {
try {
const randomWord = await fetchRandomWord(); // Fetch a random word using the API
const scrambledWord = scrambleWord(randomWord); // Scramble the word

// path not working here:
// app.patch('score', (req, res) => {
// score += parseInt(req.query.val);
// res.send(`${score}`);
// });
res.json({ scrambledWord });
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Error fetching a random word from the API' });
}
});

app.get('/score', (req, res) => {
res.send(`${score}`);
});

// eventually you will need to send the backend a guess, and tell them whether or not it was wrong.
// one way is through status codes, here is one way:
// Increment or decrement the score based on the query parameter
app.patch('/score', (req, res) => {
score += parseInt(req.query.val);
res.status(250).send(`${score}`);
const val = parseInt(req.query.val);
if (!isNaN(val)) {
score += val;
res.status(200).json({ score, guessedWords });
} else {
res.status(400).json({ message: 'Invalid score value' });
}
});
// different number ranges mean different things. Will have to do it for later endpoints.


// one more package cors

app.listen(PORT, () => {
console.log(`Backend is running on https://localhost:${PORT}`);
})

// nodemon is not working
console.log(`Backend is running on http://localhost:${PORT}`);
});
71 changes: 71 additions & 0 deletions backend/node_modules/.package-lock.json

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

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

Loading

0 comments on commit 57ea05e

Please sign in to comment.