Skip to content

Instantly share code, notes, and snippets.

@codecademydev

codecademydev/server.js Secret

Created Mar 22, 2020
Embed
What would you like to do?
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}.`)});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.