-
-
Save codecademydev/e6d6174fcff2d241a217fb80d9459edc to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const app = express(); | |
const { quotes } = require("./data"); | |
const { getRandomElement } = require("./utils"); | |
const PORT = process.env.PORT || 4001; | |
app.use(express.static("public")); | |
app.listen(PORT, () => { | |
console.log(`Listening on port ${PORT}`); | |
}); | |
app.get("/api/quotes/random", (req, res, next) => { | |
const randomQuote = getRandomElement(quotes); | |
res.send({ | |
quote: randomQuote, | |
}); | |
}); | |
app.get("/api/quotes", (req, res, next) => { | |
if (!req.query.person) { | |
res.send({ | |
quotes: quotes, | |
}); | |
} else { | |
let quotesPerAuthor = []; | |
const authorName = req.query.person; | |
for (let i = 0; i < quotes.length; i++) { | |
if (quotes[i].person === authorName) { | |
quotesPerAuthor.push(quotes[i]); | |
} | |
} | |
res.send({ | |
quotes: quotesPerAuthor, | |
}); | |
} | |
}); | |
app.post("/api/quotes", (req, res, next) => { | |
if (!req.query.quote || !req.query.person) { | |
res.status(400).send("Check your entries"); | |
} else { | |
quotes.push(req.query); | |
res.send({ | |
quote: req.query, | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment