oauo

Codecademy Quote API Challenge Project [JS] Intended Solutio

May 16th, 2020
21
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3.  
  4. const { quotes } = require('./data');
  5. const { getRandomElement } = require('./utils');
  6.  
  7. const PORT = process.env.PORT || 4001;
  8.  
  9. app.use(express.static('public'));
  10.  
  11. app.get('/api/quotes/random', (_, res) => {
  12.     res.send({quote:getRandomElement(quotes)})
  13. })
  14.  
  15. app.get('/api/quotes', (req, res) => {
  16.     if(req.query.person) {
  17.         res.send({quotes:quotes.filter(x => x.person === req.query.person)})
  18.     } else {
  19.         res.send({quotes})
  20.     }
  21. })
  22.  
  23. app.post('/api/quotes', (req, res, next) => {
  24.     const quote = req.query.quote
  25.     const person = req.query.person
  26.     if(quote && person) {
  27.         quotes.push({quote, person})
  28.         res.send({quote:{quote, person}})
  29.     } else {
  30.         res.sendStatus(400)
  31.     }
  32. })
  33.  
  34. app.listen(PORT, () => {console.log(`Listening on port: ${PORT}.`))
RAW Paste Data