-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
68 lines (58 loc) · 1.47 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
const express = require("express");
const {
getTopics,
getArticles,
getArticleById,
getCommentsByArticleId,
patchArticle,
postComment,
getUsers,
deleteComment,
getJson,
seedDbs,
} = require("./controllers/news.js");
const app = express();
app.use(express.json());
app.get("/api", getJson);
app.get("/api/seed", seedDbs);
app.get("/api/topics", getTopics);
app.get("/api/articles", getArticles);
app.get("/api/articles/:article_id", getArticleById);
app.get("/api/articles/:article_id/comments", getCommentsByArticleId);
app.get("/api/users", getUsers);
app.post("/api/articles/:article_id/comments", postComment);
app.patch("/api/articles/:article_id", patchArticle);
app.delete("/api/comments/:comment_id", deleteComment);
app.use((err, req, res, next) => {
if (err.msg && err.status) {
res.status(err.status).send({ msg: err.msg });
} else {
next(err);
}
});
app.use((err, req, res, next) => {
if (err.code === "23502") {
res.status(400).send({ msg: "missing required field" });
} else {
next(err);
}
});
app.use((err, req, res, next) => {
if (err.code === "23503") {
res.status(400).send({ msg: "invalid foreign key" });
} else {
next(err);
}
});
app.use((err, req, res, next) => {
if (err.code === "22P02") {
res.status(400).send({ msg: "incorrect data type passed" });
} else {
next(err)
}
});
app.use((err,req,res,next) => {
console.log(err)
res.status(500).send({msg: "Server error"})
})
module.exports = app;