-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (59 loc) · 2.09 KB
/
server.js
File metadata and controls
70 lines (59 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// server.js
// This is where your node app starts
//load the 'express' module which makes writing webservers easy
import express from "express";
import fetch from "node-fetch";
//load the quotes JSON
// import quotes from "./quotes.json" assert { type: "json" };
let quotes = [];
const fetchQuotes = async () => {
try {
const quotesApi = await fetch("https://api.quotable.io/quotes?page=1");
const quotesData = await quotesApi.json();
quotes = quotesData.results;
} catch (error) {
console.error("Error fetching quotes:", error);
}
};
fetchQuotes();
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");
});
app.get("/quotes", (request, response) => {
response.send({ quotes });
console.log(request.query);
});
app.get("/quotes/random", (request, response) => {
response.send(pickFromArray(quotes));
});
app.get("/quotes/search", (request, response) => {
const searchQuery = request.query.term;
console.log(searchQuery);
const searchQueryCase = searchQuery.toLocaleLowerCase();
console.log(searchQuery);
const filteredQuotes = quotes.filter((quote) => {
// console.log(quote.author);
return (
quote.content.toLocaleLowerCase().includes(searchQueryCase) ||
quote.author.toLocaleLowerCase().includes(searchQueryCase)
);
});
response.send(filteredQuotes);
});
//START OF YOUR CODE...
//...END OF YOUR CODE
//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)];
//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);
});