-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-old.js
90 lines (77 loc) · 2.17 KB
/
app-old.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
80
81
82
83
84
85
86
87
88
89
90
const { getAPI } = require('./controllers/api.controller')
const {
getArticleId,
getArticle,
getArticlesComment,
postCommentByArticleID,
patchArticle,
postArticle,
} = require('./controllers/articles.controller')
const {
deleteComment,
patchComment,
} = require('./controllers/comments.controller')
const { getTopics } = require('./controllers/topics.controllers')
const express = require('express')
const { getUsers, getUsername } = require('./controllers/users.controllers')
const app = express()
app.use(express.json())
//TOPICS
app.get('/api/topics', getTopics)
//API
app.get('/api', getAPI)
//ARTICLES
app.get('/api/articles/', getArticle)
app.post('/api/articles', postArticle)
app.get('/api/articles/:article_id', getArticleId)
app.patch('/api/articles/:article_id', patchArticle)
app.get('/api/articles/:article_id/comments', getArticlesComment)
app.post('/api/articles/:article_id/comments', postCommentByArticleID)
//USERS
app.get('/api/users', getUsers)
app.get('/api/users/:username', getUsername)
//COMMENTS
app.delete('/api/comments/:comment_id', deleteComment)
app.patch('/api/comments/:comment_id', patchComment)
//ERROR HANDLING
////PSQL errors
app.all('*', (req, res) => {
res.status(404).send({ msg: 'endpoint not found' })
})
//PSQL ERRORS
app.use((err, req, res, next) => {
if (err.code === '22003') {
res
.status(400)
.send({ msg: 'Bad request: ID is out of range for type integer' })
} else {
next(err)
}
})
app.use((err, req, res, next) => {
if (err.code === '22P02') {
res.status(400).send({ msg: 'Bad request: Not valid type of input' })
} else if (err.code === '23503') {
//Key (article_id)=(10000) is not present in table "articles".
//'Key (author)=(Demiurge) is not present in table "users".
res
.status(404)
.send({ msg: 'One of your parameters does not exist in our database' })
} else {
next(err)
}
})
////PERSONALISED ERRORS
app.use((err, req, res, next) => {
if (err.status && err.msg) {
res.status(err.status).send({ msg: err.msg })
} else {
next(err)
}
})
//////Last-Error Resource
app.use((err, req, res, next) => {
// console.log(err)
res.status(500).send({ msg: "server error! We're very sorry" })
})
module.exports = app