Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 24, 2020 19:14
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/413a6f452cfccd21d015125885968ac4 to your computer and use it in GitHub Desktop.
Save codecademydev/413a6f452cfccd21d015125885968ac4 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);
res.send({quote: randomQuote});
});
app.get('/api/quotes', (req, res, next) => {
const newArray = quotes.filter(event => event.person === req.query.person);
if(newArray) {
res.send({quotes: newArray});
} else {
res.status(404).send();
}
});
app.post('/api/quotes', (req, res, next) => {
const newQuote = {quote: req.query.quote, person: req.query.person};
if(req.query.quote && req.query.person){
quotes.push(newQuote);
res.status(201).send({quote: newQuote})
} else {
res.status(400).send();
}
});
app.listen(PORT, () => {
console.log(`Listening on port ${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