-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (60 loc) · 2.08 KB
/
app.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
const cors = require('cors');
const express = require("express");
const app = express();
const {getTopics, getApi, getArticleByID, getArticles, getCommentsByID, postComment, getUsers, getUser, patchArticleVotes, deleteComment, patchCommentVotes, postArticle} = require("./Controllers");
app.use(cors());
app.use(express.json());
//Happy paths
app.get("/api", getApi);
app.get("/api/topics", getTopics);
app.get("/api/articles", getArticles)
app.post("/api/articles", postArticle)
app.get("/api/articles/:article_id", getArticleByID)
app.patch("/api/articles/:article_id", patchArticleVotes)
app.get('/api/articles/:article_id/comments', getCommentsByID);
app.post("/api/articles/:article_id/comments", postComment)
app.patch("/api/comments/:comment_id", patchCommentVotes)
app.delete("/api/comments/:comment_id", deleteComment)
app.get("/api/users", getUsers)
app.get("/api/users/:username", getUser)
//Path not found error
app.use((req, res) => {
res.status(404).send({ msg: 'Path not found' })
});
//Error handling middleware functions
app.use((err, req, res, next) => {
//user error
if (err.status) {
res.status(err.status).send({ msg: err.msg });
} else next(err);
});
app.use((err, req, res, next) => {
//psql user related error
if(err.code === '22P02' ){
res.status(400).send({msg : 'Invalid text representation'});
} else next(err);
});
app.use((err, req, res, next) => {
//psql user related error
if(err.code === '23503' ){
res.status(400).send({msg : 'Foreign key violation'});
} else next(err);
});
app.use((err, req, res, next) => {
//psql user related error
if(err.code === '23502' ){
res.status(400).send({msg : 'Not null violation'});
} else next(err);
});
app.use((err, req, res, next) => {
//psql user related error
if(err.code === '42703' ){
res.status(404).send({msg : 'Column does not exist'});
} else next(err);
});
//Internal system error if no catches are made
app.use((err, req, res, next) => {
// console.log(err);
res.status(500).send({ msg: 'Internal Server Error' });
});
module.exports = app;