-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
729 changed files
with
197,722 additions
and
61,448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
node_modules/ms/license.md → backend/node_modules/asynckit/LICENSE
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.