Skip to content
Open
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
19 changes: 12 additions & 7 deletions mailing-list-api/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}

60 changes: 60 additions & 0 deletions mailing-list-api/server.js
Original file line number Diff line number Diff line change
@@ -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"]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

THis is an intresting approach - is there any particulaur reason you moved the data out of the file?


// 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" });
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work! APprecate the comments :)


// 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` });
}
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work glad to see you did the stretch goal :)


const PORT = 3003;
const server = app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});

export default server;
50 changes: 26 additions & 24 deletions quote-server/server.js
Original file line number Diff line number Diff line change
@@ -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}`);
});