Skip to content

Instantly share code, notes, and snippets.

@codecademydev

codecademydev/server.js Secret

Created Jul 21, 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');
app.get("/api/quotes/random",(req,res,next)=>{
const quote=getRandomElement(quotes);
res.status(200).send({quote:quote});
})
app.get("/api/quotes",(req,res,next)=>{
if(!req.query.person){
return res.send({quotes:quotes});
}
const filteredQuote=quotes.filter((quote)=>{
return quote.person==req.query.person;
});
res.send({quotes:filteredQuote})
})
app.post("/api/quotes",(req,res,next)=>{
if(req.query.quote && req.query.person){
quotes.push(req.query)
res.status(201).send(req.query);
}
else{
res.sendStatus(400);
}
})
const PORT = process.env.PORT || 4001;
app.use(express.static('public'));
app.listen(PORT,()=>{
console.log("Server Initiated ");
});
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.