diff --git a/index.html b/index.html index ad695af..0b4efe0 100644 --- a/index.html +++ b/index.html @@ -48,8 +48,10 @@ - - + +
+

+
diff --git a/package.json b/package.json index b6e73d8..3bc1400 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,20 @@ { + "type": "module", "name": "api-project--egbie-ollie", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "nodemon server.js" }, "keywords": [], "author": "", "license": "ISC", "devDependencies": { "eslint": "^8.57.0", - "express": "^4.18.3" + "express": "^4.18.3", + "node-fetch": "^3.3.2", + "nodemon": "^3.1.0" } } diff --git a/server.js b/server.js new file mode 100644 index 0000000..1da5818 --- /dev/null +++ b/server.js @@ -0,0 +1,34 @@ +import fetch from "node-fetch"; // for HTTP requests +import express from "express"; // for HTTP requests +import path from "path"; +import { fileURLToPath } from 'url'; + +// Workaround for __dirname in ES modules +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + +const app = express(); +const PORT = process.env.PORT || 3000; + +// Serve static files from the "static" directory +app.use(express.static(path.join(__dirname, "static"))); + +// Route to handle requests for jokes +app.get("/joke", async (req, res) => { + try { + // Request to JokeAPI + const response = await fetch("https://v2.jokeapi.dev/joke/Any?contains=pint"); + const data = await response.json(); + const joke = data.contents.jokes[0].joke.text; + + // Send the joke back as JSON + res.json({ joke }); + } catch (error) { + console.error("Error fetching joke:", error); + res.status(500).json({ error: "Failed to fetch joke" }); + } +}); + +app.listen(PORT, () => { + console.log(`Server is listening on port ${PORT}`); +}); \ No newline at end of file diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..5ced246 --- /dev/null +++ b/static/index.html @@ -0,0 +1,119 @@ + + + + + + + + Clever name + + + + + + +
+ +
+ +
+ + + +
+

+
+ + + + + +
+ + +
+ + +
+ + + + + + \ No newline at end of file diff --git a/static/js/script.js b/static/js/script.js index e69de29..7cdf3ee 100644 --- a/static/js/script.js +++ b/static/js/script.js @@ -0,0 +1,14 @@ +document.addEventListener("DOMContentLoaded", () => { + const jokeElement = document.getElementById("joke"); + + // Fetch joke from server and update HTML + fetch("/joke") + .then((response) => response.json()) + .then((data) => { + jokeElement.textContent = data.joke; + }) + .catch((error) => { + console.error("Error fetching joke:", error); + jokeElement.textContent = "Failed to fetch joke"; + }); + }); \ No newline at end of file