Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created July 27, 2020 22:05
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/eb795ce218716418d55837d2a85dc2e6 to your computer and use it in GitHub Desktop.
Save codecademydev/eb795ce218716418d55837d2a85dc2e6 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.get('/api/quotes/random', (req, res, next) => {
const randomQuote = getRandomElement(quotes);
//delete randomQuote.person;
const quoteObject = {quote: randomQuote};
//console.log(quoteObject);
res.send(quoteObject);
});
app.get('/api/quotes', (req, res, next) => {
const quotePerson = req.query.person;
//console.log(quotePerson);
if(quotePerson) {
const newQuotes = quotes.filter( (arr) => {
return arr.person === quotePerson;
});
//console.log(newQuotes);
const quoteObjects = {quotes: newQuotes};
res.send(quoteObjects);
} else {
const quoteObjects = {quotes: quotes};
//console.log(quoteObjects);
res.send(quoteObjects);
}
});
app.post('/api/quotes', (req, res, next) => {
const newText = req.query.quote;
const newPerson = req.query.person;
//console.log(`${newText}, ${newPerson}`);
if(newText && newPerson) {
const newQuote = {quote: newText, person: newPerson};
//console.log(newQuote);
quotes.push(newQuote);
const quoteObject = {quote: newQuote};
//console.log(quoteObject);
res.send(quoteObject);
} else {
res.status(400).send();
}
})
app.listen(PORT, () => {
console.log(`Server is listening on ${PORT}`);
});
const getRandomElement = arr => {
if (!Array.isArray(arr)) throw new Error('Expected an array');
return arr[Math.floor(Math.random() * arr.length)];
}
module.exports = {
getRandomElement
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment