Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 22, 2020 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/459714bfa9fb76f4d326bc8600aa2fe3 to your computer and use it in GitHub Desktop.
Save codecademydev/459714bfa9fb76f4d326bc8600aa2fe3 to your computer and use it in GitHub Desktop.
Codecademy export
const express = require('express');
const morgan = require('morgan');
const app = express();
const { quotes } = require('./data');
const { getRandomElement } = require('./utils');
const PORT = process.env.PORT || 4001;
app.use(express.static('public'));
app.get('/api/quotes/random', (req, res, next) => {
const quote = getRandomElement(quotes);
res.send({quote: quote});
});
app.get('/api/quotes', (req, res, next) => {
if (!req.query.hasOwnProperty('person')) {
res.send({quotes: quotes});
} else {
const filterQuote = quotes.filter(element => element.person === req.query.person);
res.send({quotes: filterQuote});
}
});
app.post('/api/quotes', (req, res, next) => {
if (req.query.quote && req.query.person) {
const newQuote = {quote: req.query.quote, person: req.query.person};
quotes.push(newQuote);
res.send({quote: newQuote});
}
else {
res.status(400).send();
}
});
app.listen(PORT, () => {console.log(`Server Quote API listening on port:${PORT}.`)});
@Anthony-Chow
Copy link

Wow! Thanks so much. It helps me a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment