-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
64 lines (45 loc) · 1.32 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
const express = require("express");
const cors = require("cors");
const {
getTopics,
getArticleById,
updateVotes,
getUsers,
getArticles,
getArticleComments,
postComment,
deleteComment,
root,
paths,
} = require("./controller");
const app = express();
app.use(cors());
app.use(express.json());
app.get("/", root);
app.get("/api", paths);
app.get("/api/topics", getTopics);
app.get("/api/articles/:articleId", getArticleById);
app.patch("/api/articles/:articleId", updateVotes);
app.get("/api/users", getUsers);
app.get("/api/articles", getArticles);
app.get("/api/articles/:articleId/comments", getArticleComments);
app.post("/api/articles/:articleId/comments", postComment);
app.delete("/api/comments/:commentId", deleteComment);
app.use((err, req, res, next) => {
// handle custom errors
if (err.status && err.msg) {
res.status(err.status).send({ msg: err.msg });
} else next(err);
});
app.use((err, req, res, next) => {
if (err.code === "22P02" || err.msg === "invalid input type") {
res.status(400).send({ msg: "invalid input type" });
} else next(err);
});
app.use((err, req, res, next) => {
res.status(500).send({ msg: "Internal Servers Error" });
});
app.use("*", (request, response) => {
response.status(404).send({ message: "404: endpoint does not exist" });
});
module.exports = app;