-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
46 lines (38 loc) · 1006 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
44
45
const express = require('express');
const morgan = require('morgan');
const app = express();
const { quotes } = require('../data');
const { getRandomElement } = require('../utils');
const PORT = process.env.PORT || 4000;
app.use(express.static('public'));
app.get('/api/quotes/random', (req, res) => {
res.send({
quote: getRandomElement(quotes)
});
});
app.get('/api/quotes', (req, res, next) => {
if (req.query.person !== undefined) {
const quotesByPerson = quotes.filter(quote => quote.person === req.query.person);
res.send({
quotes: quotesByPerson
});
} else {
res.send({quotes:quotes
});
}
});
app.post('/api/quotes', (req, res) => {
const newQuote = {
quote: req.query.quote,
person: req.query.person
};
if (newQuote.quote && newQuote.person) {
quotes.push(newQuote);
res.send({ quote: newQuote });
} else {
res.status(400).send();
}
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}.`);
});