Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 22, 2020 22:52
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/6aed2f1b4498f064201814bcf8f8d9e7 to your computer and use it in GitHub Desktop.
Save codecademydev/6aed2f1b4498f064201814bcf8f8d9e7 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.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
//GET Random quote
app.get('/api/quotes/random', (req, res, next) => {
res.send({
quote: getRandomElement(quotes)
});
});
//GET all quotes
app.get('/api/quotes', (req, res, next) => {
if(req.query.person !== undefined){
const findQuote = quotes.filter(quote => quote.person === req.query.person);
res.send({
quotes: findQuote
});
}else {
res.send({
quotes: quotes
});
}
})
//POST Create new quotes
app.post('/api/quotes',(req, res, next) => {
const newElement = {
quote: req.query.quote,
person: req.query.person
};
if(newElement.quote && newElement.person) {
quotes.push(newElement);
res.send({quote:newElement})
}else {
res.status(400).send();
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment