Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.
Open
Changes from 1 commit
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
44 changes: 43 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const express = require("express");
const app = express();


//load the quotes JSON
const quotes = require("./quotes.json");

Expand All @@ -13,11 +14,50 @@ const quotes = require("./quotes.json");
// /quotes - Should return all quotes (json)
// /quotes/random - Should return ONE quote (json)
app.get("/", function (request, response) {
response.send("Neill's Quote Server! Ask me for /quotes/random, or /quotes");
response.send("Saliha's Quote Server! Ask me for /quotes/random, or /quotes");
});

//START OF YOUR CODE...

app.get("/quotes", function (request, response) {
response.send({quotes});
});

app.get("/quotes/random", function (request, response) {

// Pick a random quote from the array using the pickFromArray function
const randomQuote = pickFromArray(quotes);

// Send the random quote as the response
response.send(randomQuote);
});

// http://localhost:62297/quotes/search?term=life/success/miss
// app.get("/quotes/search", function (request, response) {
// // let term = request.query.term
// // response.send(term);
// });

// advance
app.get("/quotes/search", function (request, response) {
const term = request.query.term;
const searchResults = searchQuotes(term);
response.send({searchResults});
Copy link

@migmow migmow Jul 21, 2023

Choose a reason for hiding this comment

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

I'd recommend using .json when you're sending JSON data though, because it makes it obvious to other developers that you are aiming to send JSON data back.
Would be great if you study this => https://expressjs.com/en/api.html#res.send and see the difference between .send and .json

});


// ## Level 2 Challenge - allow quote _searches_!
function searchQuotes(term) {
const results = quotes.filter(
(quote) =>
Copy link

Choose a reason for hiding this comment

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

Can you think of a situation where term is undefined? can you handle it by using response.status(400) and sending a message as well?

quote.quote.toLowerCase().includes(term.toLowerCase()) ||
quote.author.toLowerCase().includes(term.toLowerCase()) ||
quote === ""
Copy link

@migmow migmow Jul 21, 2023

Choose a reason for hiding this comment

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

Why are you filitering empty quotes here?

);
return results;
}


//...END OF YOUR CODE

//You can use this function to pick one element at random from a given array
Expand All @@ -32,3 +72,5 @@ function pickFromArray(arr) {
const listener = app.listen(process.env.PORT, function () {
console.log("Your app is listening on port " + listener.address().port);
});