From e95e1629def46154be25f4b2fb0cfd025198e0da Mon Sep 17 00:00:00 2001 From: Fathi Kahin <127356471+fhkahin@users.noreply.github.com> Date: Mon, 8 Apr 2024 22:29:21 +0100 Subject: [PATCH 1/3] Updated server.js --- quote-server/server.js | 50 ++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/quote-server/server.js b/quote-server/server.js index 8a51778..0c4c028 100644 --- a/quote-server/server.js +++ b/quote-server/server.js @@ -1,32 +1,34 @@ -// server.js -// This is where your node app starts - -//load the 'express' module which makes writing webservers easy import express from "express"; -//load the quotes JSON -import quotes from "./quotes.json" assert { type: "json" }; +import fetch from "node-fetch"; +import cors from "cors"; const app = express(); -// Now register handlers for some routes: -// / - Return some helpful welcome info (text) -// /quotes - Should return all quotes (json) -// /quotes/random - Should return ONE quote (json) -app.get("/", (request, response) => { - response.send("Neill's Quote Server! Ask me for /quotes/random, or /quotes"); -}); -//START OF YOUR CODE... -//...END OF YOUR CODE +app.use(cors()); + +const getQuotes = async (url) => { + const response = await fetch(url); + return response.json(); +}; + +app.get("/", (req, res) => { + res.send("Welcome to Fathi's Quote Server! Use /quotes/random or /quotes to get quotes."); +}); + +app.get("/quotes", async (req, res) => { + const pageNumber = req.query.page || 1; + const allQuotes = await getQuotes(`https://api.quotable.io/quotes?page=${pageNumber}`); + res.send(allQuotes); +}); -//You can use this function to pick one element at random from a given array -//example: pickFromArray([1,2,3,4]), or -//example: pickFromArray(myContactsArray) -// -const pickFromArray = (arrayofQuotes) => - arrayofQuotes[Math.floor(Math.random() * arrayofQuotes.length)]; +app.get("/quotes/random", async (req, res) => { + const randomQuoteResponse = await fetch("https://api.quotable.io/random"); + const randomQuote = await randomQuoteResponse.json(); + res.send(randomQuote); +}); -//Start our server so that it listens for HTTP requests! -const listener = app.listen(3001, () => { - console.log("Your app is listening on port " + listener.address().port); +const PORT = process.env.PORT || 3001; +app.listen(PORT, () => { + console.log(`Quote server is running on port ${PORT}`); }); From 44c0d39a75df24325733b6bff5b6c3eec1e4a474 Mon Sep 17 00:00:00 2001 From: Fathi Kahin <127356471+fhkahin@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:18:06 +0100 Subject: [PATCH 2/3] Completed server.js --- mailing-list-api/server.js | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 mailing-list-api/server.js diff --git a/mailing-list-api/server.js b/mailing-list-api/server.js new file mode 100644 index 0000000..fd2cca7 --- /dev/null +++ b/mailing-list-api/server.js @@ -0,0 +1,60 @@ +import express from "express"; + +const app = express(); +app.use(express.json()); + +const lists = new Map(); +lists.set("staff", ["talea@techtonica.org", "michelle@techtonica.org"]); +lists.set("cohort-h1-2020", ["ali@techtonica.org", "humail@techtonica.org", "khadar@techtonica.org"]); + +// Get all lists +app.get("/lists", (req, res) => { + const listsArray = Array.from(lists.keys()); + res.status(200).json({ lists: listsArray }); +}); + +// Get members of a specific list +app.get("/lists/:name", (req, res) => { + const listName = req.params.name; + const list = lists.get(listName); + if (list) { + res.status(200).json({ name: listName, members: list }); + } else { + res.status(404).json({ error: "List not found" }); + } +}); + +// Delete a list +app.delete("/lists/:name", (req, res) => { + const listName = req.params.name; + if (lists.delete(listName)) { + res.status(200).json({ message: `Deleted ${listName} successfully!` }); + } else { + res.status(404).json({ error: "List not found" }); + } +}); + +// Update or create a list +app.put("/lists/:name", (req, res) => { + const listName = req.params.name; + const { name, members } = req.body; + + if (listName.toLowerCase() !== name.toLowerCase()) { + res.status(400).json({ error: `Path (${listName}) & JSON body ("name": ${name}) do not match` }); + return; + } + + lists.set(listName, members); + if (lists.has(listName)) { + res.status(200).json({ message: `List ${listName} has been updated` }); + } else { + res.status(201).json({ message: `New list ${listName} has been created` }); + } +}); + +const PORT = 3003; +const server = app.listen(PORT, () => { + console.log(`Server is listening on port ${PORT}`); +}); + +export default server; From 24b57f4997227101519190ff49710da6b2a50bdc Mon Sep 17 00:00:00 2001 From: Fathi Kahin <127356471+fhkahin@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:18:52 +0100 Subject: [PATCH 3/3] Updated package.json --- mailing-list-api/package.json | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mailing-list-api/package.json b/mailing-list-api/package.json index 8b70aaa..8df23a1 100644 --- a/mailing-list-api/package.json +++ b/mailing-list-api/package.json @@ -1,20 +1,25 @@ { - "name": "@module-node/mailing-list-api", + "name": "mailing-list-api", "version": "1.0.0", - "license": "CC-BY-SA-4.0", - "description": "You must update this package", + "license": "MIT", + "description": "Mailing list API for beginners", "scripts": { + "start": "node server.js", "test": "jest" }, "repository": { "type": "git", - "url": "git+https://github.com/CodeYourFuture/CYF-Coursework-Template.git" + "url": "https://github.com/your-username/your-repo" }, "bugs": { - "url": "https://github.com/CodeYourFuture/CYF-Coursework-Template/issues" + "url": "https://github.com/your-username/your-repo/issues" }, - "homepage": "https://github.com/CodeYourFuture/CYF-Coursework-Template#readme", + "homepage": "https://github.com/your-username/your-repo#readme", "devDependencies": { - "jest": "^26.6.3" + "jest": "^27.0.0" + }, + "dependencies": { + "express": "^4.17.1" } } +