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
7 changes: 5 additions & 2 deletions quote-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"type": "module",
"main": "server.js",
"scripts": {
"start": "node --no-warnings=ExperimentalWarning server.js"
"start": "nodemon --no-warnings=ExperimentalWarning server.js"
},
"license": "MIT",
"keywords": [
"node",
"glitch",
"express"
]
],
"dependencies": {
"express": "^4.19.2"
}
}
41 changes: 16 additions & 25 deletions quote-server/server.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
// 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 express from 'express';
import quotes from './quotes.json' assert { type: "json" }; // Assuming quotes.json is in the same directory

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...
const PORT = 3000;

//...END OF YOUR CODE
// Route to return all quotes
app.get('/quotes', (req, res) => {
res.json(quotes);
});

//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)];
// Route to return a random quote
app.get('/quotes/random', (req, res) => {
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
res.json(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);
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});