-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
59 lines (42 loc) · 1.27 KB
/
app.js
File metadata and controls
59 lines (42 loc) · 1.27 KB
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
const express = require("express");
const app = express();
const { getCategories } = require("./controllers/controller.categories");
const { getUsers } = require("./controllers/controller.users");
const {
getReviewById,
patchReview,
getReviews,
getCommentsForReview,
postCommentForReview
} = require("./controllers/controller.reviews");
app.use(express.json());
app.get("/api/categories", getCategories);
app.get("/api/users", getUsers);
app.get("/api/reviews", getReviews);
app.get("/api/reviews/:review_id", getReviewById);
app.get("/api/reviews/:review_id/comments", getCommentsForReview)
app.patch("/api/reviews/:review_id", patchReview)
app.post("/api/reviews/:review_id/comments", postCommentForReview)
app.all("/*", (req, res) => {
res.status(404).send({ msg: "Not Found" });
});
app.use((err, req, res, next) => {
if (err.code === "22P02") {
res.status(400).send({ msg: "Invalid Input" });
} else {
if (err.code === "23502") {
res.status(400).send({ msg: "Invalid Input" });
}
next(err);
}
});
app.use((err, req, res, next) => {
if (err.status) {
res.status(err.status).send({ msg: err.msg });
}
next(err);
});
app.use((err, req, res, next) => {
res.status(500).send({ msg: "Internal Server Error" });
});
module.exports = app;