-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.js
69 lines (59 loc) · 1.92 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
const users = require('./controllers/users');
const posts = require('./controllers/posts');
const votes = require('./controllers/votes');
const comments = require('./controllers/comments');
const requireAuth = require('./middlewares/requireAuth');
const postAuth = require('./middlewares/postAuth');
const commentAuth = require('./middlewares/commentAuth');
const router = require('express').Router();
//Authentication
router.post('/signup', users.validate, users.signup);
router.post('/authenticate', users.validate, users.authenticate);
//Posts
router.param('post', posts.load);
router.post('/posts', [requireAuth, posts.validate], posts.create);
router.get('/post/:post', posts.show);
router.get('/posts', posts.list);
router.get('/posts/:category', posts.listByCategory);
router.get('/user/:username', posts.listByUser);
router.delete('/post/:post', [requireAuth, postAuth], posts.delete);
//Post votes
router.get('/post/:post/upvote', requireAuth, votes.upvote);
router.get('/post/:post/downvote', requireAuth, votes.downvote);
router.get('/post/:post/unvote', requireAuth, votes.downvote);
//Posts comments
router.param('comment', comments.load);
router.post('/post/:post', [requireAuth, comments.validate], comments.create);
router.get(
'/post/:post/:comment/upvote',
[requireAuth],
votes.com_upvote,
);
router.get(
'/post/:post/:comment/downvote',
[requireAuth],
votes.com_downvote,
);
router.get(
'/post/:post/:comment/unvote',
[requireAuth],
votes.com_unvote,
);
router.delete(
'/post/:post/:comment',
[requireAuth, commentAuth],
comments.delete
);
module.exports = (app) => {
app.use('/api', router);
app.use((req, res, next) => {
const error = new Error('Not found');
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500).json({
message: error.message
});
});
};