-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
79 lines (66 loc) · 1.9 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require('express');
const app = express();
const { quotes } = require('./data');
const { getRandomElement, getElementById, getElementIndexById } = require('./utils');
const PORT = process.env.PORT || 4000;
app.use(express.static('public'));
app.use((req, res, next) => {
console.log(`${req.method} Request Received`)
})
app.get('/api/quote/:id', (req, res, next) => {
let ee = getElementById(req.params.id, quotes)
if (ee) {
res.status(200).send({quote: ee})
} else {
res.status(400).send()
}
})
app.get('/api/quotes/random', (req, res, next) => {
let ee = getRandomElement(quotes)
res.status(200).send({quote: ee})
})
app.get('/api/quotes', (req, res, next) => {
const person = req.params.person;
let arr = [];
quotes.forEach(element => {
arr.push(element)
});
res.status(200).send({ quotes: arr })
})
app.post('/api/quotes', (req, res, next) => {
let q1Q = req.params.quote;
let q1P = req.params.person;
if (q1Q && q1P ){
let q1 = new Quote(q1Q, q1P)
quotes.push(q1)
res.status(200).send(q1)
} else {
res.status(400).send()
}
})
app.put('/api/quote/:id', (req, res, next) => {
const index = getElementIndexById(req.params.id, quotes)
if(index) {
if(req.query.quote){
quotes[index].quote = req.query.quote;
}
if(req.query.person){
quotes[index].person = req.query.person;
}
res.status(200).send(quotes[index])
} else {
res.status(400).send()
}
})
app.delete('/api/quote/:id', (req, res, next) => {
const index = getElementIndexById(req.params.id, quotes)
if(index) {
delete quotes[index];
res.status(200).send('Resource deleted')
} else {
res.status(400).send()
}
})
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
})