Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 10, 2020 05:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codecademydev/069fe3bfab79ffcc6e0da0a06dc60c5c to your computer and use it in GitHub Desktop.
Save codecademydev/069fe3bfab79ffcc6e0da0a06dc60c5c to your computer and use it in GitHub Desktop.
Codecademy export
const quotes = [
{
quote: 'We build our computer (systems) the way we build our cities: over time, without a plan, on top of ruins.',
person: 'Ellen Ullman'
},
{
quote: 'The best thing about a boolean is even if you are wrong, you are only off by a bit.',
person: 'Anonymous'
},
{
quote: `If it's a good idea, go ahead and do it. It's much easier to apologize than it is to get permission.`,
person: 'Grace Hopper'
},
{
quote: 'The city’s central computer told you? R2D2, you know better than to trust a strange computer!',
person: 'C-3PO'
},
{
quote: 'I have always wished for my computer to be as easy to use as my telephone; my wish has come true because I can no longer figure out how to use my telephone.',
person: 'Bjarne Stroustrup'
},
{
quote: 'Understand well as I may, my comprehension can only be an infinitesimal fraction of all I want to understand.',
person: 'Ada Lovelace'
},
{
quote: 'Java is to JavaScript as ham is to hamster.',
person: 'Jeremy Keith'
},
{
quote: `The most dangerous phrase in the language is, "We've always done it this way."`,
person: 'Grace Hopper'
},
{
quote: 'As soon as we started programming, we found to our surprise that it wasn’t as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.',
person: 'Maurice Wilkes'
},
{
quote: 'Learning to write programs stretches your mind, and helps you think better, creates a way of thinking about things that I think is helpful in all domains.',
person: 'Bill Gates'
},
{
quote: 'What one programmer can do in one month, two programmers can do in two months.',
person: 'Fred Brooks'
},
{
quote: 'The Internet? Is that thing still around?',
person: 'Homer Simpson'
},
{
quote: 'If you tell me precisely what it is a machine cannot do, then I can always make a machine which will do just that.',
person: 'Jon von Neumann'
},
];
module.exports = {
quotes
};
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}`);
});
// GET a random Quote.
app.get('/api/quotes/random', (req, res) => {
const randomQuote = getRandomElement(quotes);
res.send({ quote: randomQuote });
});
//GET all quotes or all quotes from an author.
app.get('/api/quotes', (req, res) => {
const filterQuotes = quotes.filter(author => {
return author.person === req.query.person;
});
if (req.query.person) {
res.send({ quotes: filterQuotes });
} else {
res.send({ quotes: quotes });
}
});
//POST for adding new quotes.
app.post('/api/quotes', (req, res) => {
const newQuote = req.query.quote;
const newPerson = req.query.person;
if (newQuote != '' && newPerson != '') {
quotes.push({ quote: newQuote, person: newPerson });
res.send({ quote: { quote: newQuote, person: newPerson } });
} else {
res.sendStatus(400);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment