-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
38 lines (32 loc) · 1.37 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
const users = require('./controllers/users');
const posts = require('./controllers/posts');
const comments = require('./controllers/comments');
const { jwtAuth, postAuth, commentAuth } = require('./auth');
const router = require('express').Router();
router.post('/login', users.validate(), users.login);
router.post('/register', users.validate('register'), users.register);
router.param('post', posts.load);
router.get('/posts', posts.list);
router.get('/posts/:category', posts.listByCategory);
router.get('/post/:post', posts.show);
router.post('/posts', [jwtAuth, posts.validate], posts.create);
router.delete('/post/:post', [jwtAuth, postAuth], posts.destroy);
router.get('/post/:post/upvote', jwtAuth, posts.upvote);
router.get('/post/:post/downvote', jwtAuth, posts.downvote);
router.get('/post/:post/unvote', jwtAuth, posts.unvote);
router.get('/user/:user', posts.listByUser);
router.param('comment', comments.load);
router.post('/post/:post', [jwtAuth, comments.validate], comments.create);
router.delete('/post/:post/:comment', [jwtAuth, commentAuth], comments.destroy);
module.exports = app => {
app.use('/api', router);
app.get('*', (req, res) => {
res.status(404).json({ message: 'not found' });
});
app.use((err, req, res, next) => {
if (err.type === 'entity.parse.failed') {
return res.status(400).json({ message: 'bad request' });
}
next(err);
});
};