Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joke fixes #34

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions static/index.html → public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/styles.css">
<script src="js/script.js" defer></script>
<link rel="stylesheet" href="/css/styles.css">
<script src="/js/script.js" defer></script>
<title>Clever name</title>
</head>
<body>
Expand Down Expand Up @@ -53,7 +53,6 @@
<p id="joke"></p>
</div>





Expand Down
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ const PORT = process.env.PORT || 3000;
// Serve static files from the "static" directory
app.use(bodyParser.urlencoded({extended: true }));
app.use(express.static("static"));
app.use(express.static("public"));

// Route to handle search submission
app.get("/search", async (req, res) => {
const searchTerm = req.query.searchTerm;
console.log(`searchTerm: ${searchTerm}`)

try {
// Make a request to the JokeAPI with the search term
Expand Down
12 changes: 8 additions & 4 deletions static/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ document.addEventListener("DOMContentLoaded", () => {

// Function to handle form submission
const handleFormSubmit = async (event) => {
console.log("Submit happened")
event.preventDefault(); // Prevent the default form submission behavior

const searchTerm = searchInput.value; // Get the search term from the input field
try {
// Make a GET request to the server with search term in route
const response = await fetch(`/search?searchTerm=${encodeURIComponent(searchTerm)}`, {
method: "GET"
});

console.log(`response: ${response}`)
// Check if response is successful
if (!response.ok) {
throw new Error("Failed to fetch joke");
}

// Parse the JSON response to get joke data
const data = await response.json();

// Deal with different joke structures
if (data.type === "single") {
jokeElement.textContent = data.joke;
} else if (data.type === "twopart") {
Expand All @@ -27,8 +30,9 @@ document.addEventListener("DOMContentLoaded", () => {
throw new Error("Unknown joke format");
}
} catch (error) {
// Catch errors that might occur during fetch or parsing
console.error("Error fetching joke:", error);
jokeElement.textContent = "Failed to fetch joke";
jokeElement.textContent = "Sorry, we couldn't find a relevant joke!";
}
};

Expand Down
Empty file removed static/scripts/script.js
Empty file.