-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
43 lines (34 loc) · 1015 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const express = require('express');
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 quotation = getRandomElement(quotes);
res.send({
quote: quotation
});
});
app.get('/api/quotes', (req, res, next) => {
const personToQuote = req.query.person;
if (personToQuote) {
const quotesPerPerson = quotes.filter(quote => quote.person === personToQuote);
res.send({ quotes: quotesPerPerson})
} else {
res.send({ quotes: []})
}
});
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.send({quote: newQuote});
} else {
res.status(400).send();
}
});
app.listen(PORT);