-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
75 lines (67 loc) · 1.85 KB
/
routes.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
const express = require('express')
const router = express.Router()
// GET /questions
// Route for questions collection
router.get('/', (req, res) => res.json({ response: 'You sent me a GET request' }))
// POST /questions
// Route for creating questions
router.post('/', (req, res) => {
res.json({
response: 'You sent me a POST request',
body: req.body,
})
})
// GET /questions/:id
// Route for specific questions
router.get('/:qID', (req, res) => res.json({ response: `You sent me a GET request for ID ${req.params.qID}` }))
// POST /questions/:id/answers
// Route for creating an answer
router.post('/:qID/answers', (req, res) => {
res.json({
response: 'You sent me a POST request to /answers',
questionId: req.params.qID,
body: req.body,
})
})
// PUT /questions/:qID/answers/:aID
// Edit a specific answer
router.put('/:qID/answers/:aID', (req, res) => {
res.json({
response: 'You sent me a PUT request to /answers',
questionId: req.params.qID,
answerId: req.params.aID,
body: req.body,
})
})
// DELETE /questions/:qID/answers/:aID
// Delete a specific answer
router.delete('/:qID/answers/:aID', (req, res) => {
res.json({
response: 'You sent me a DELETE request to /answers',
questionId: req.params.qID,
answerId: req.params.aID,
})
})
// POST /questions/:qID/answers/:aID/vote-up
// POST /questions/:qID/answers/:aID/vote-down
// Vote on a specific answer
router.post(
'/:qID/answers/:aID/vote-:dir',
(req, res, next) => {
if (req.params.dir.search(/^(up|down)$/) === -1) {
const err = new Error('Not Found')
err.status = 404
next(err)
}
next()
},
(req, res) => {
res.json({
response: `You sent me a POST request to /vote-${req.params.dir}`,
questionId: req.params.qID,
answerId: req.params.aID,
vote: req.params.dir,
})
},
)
module.exports = router