-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
105 lines (87 loc) · 2.87 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const endpointsJson = require("./endpoints.json");
const db = require("./db/connection");
const cors = require("cors");
const express = require("express");
const { getTopics } = require("./src/__controllers__/topics");
const {
getArticleById,
getArticles,
patchArticle,
} = require("./src/__controllers__/articles");
const {
getComments,
postComment,
deleteComment,
patchComment,
getCommentById,
} = require("./src/__controllers__/comments");
const { getUsers, getUserByID } = require("./src/__controllers__/users");
const app = express();
app.use(cors());
app.use(express.json());
// GET endpoints start
app.get("/api", (request, response, next) => {
response.status(200).send({ endpoints: endpointsJson });
});
app.get("/api/topics", getTopics);
app.get("/api/articles/:article_id/comments", getComments);
app.get("/api/comment/:comment_id", getCommentById);
app.get("/api/articles/:article_id", getArticleById);
app.get("/api/articles", getArticles);
app.get("/api/users", getUsers);
app.get("/api/users/:username", getUserByID);
// GET endpoints end
// POST endpoints start
app.post("/api/articles/:article_id/comments", postComment);
// POST endpoints end
// PATCH endpoints start
app.patch("/api/articles/:article_id", patchArticle);
app.patch("/api/comment/:comment_id", patchComment);
// PATCH endpoints end
// DELETE endpoints start
app.delete("/api/comments/:comment_id", deleteComment);
// DELETE endpoints end
// Default responses
app.get("*", (request, response, next) => {
response.status(404).send({ error: "Not Found" });
});
app.post("*", (request, response, next) => {
response.status(405).send({ error: "Method Not Allowed" });
});
app.patch("*", (request, response, next) => {
response.status(405).send({ error: "Method Not Allowed" });
});
app.delete("*", (request, response, next) => {
response.status(405).send({ error: "Method Not Allowed" });
});
// Error Handling
app.use((error, request, response, next) => {
if (error.status && error.msg) {
response.status(error.status).send({ error: error.error, msg: error.msg });
} else next(error);
});
app.use((error, request, response, next) => {
if (error.code === "22P02") {
response.status(400).send({ error: "Bad Request", msg: "Invalid ID" });
} else next(error);
});
app.use((error, request, response, next) => {
if (
error.code === "23503" &&
error.constraint === "comments_article_id_fkey"
) {
response
.status(404)
.send({ error: "Not Found", msg: "Article does not exist" });
} else next(error);
});
app.use((error, request, response, next) => {
if (error.code === "23503" && error.constraint === "comments_author_fkey") {
response.status(401).send({ error: "Unauthorised" });
} else next(error);
});
app.use((error, request, response, next) => {
console.log(error);
response.status(500).send({ error: "Internal Server Error" });
});
module.exports = app;