-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (53 loc) · 1.59 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
const express = require("express");
const cors = require("cors");
const app = new express();
app.use(cors());
app.use(express.json());
const { readFile } = require("fs/promises");
const {
getArticles,
getArticlesById,
getCommentsByArticleId,
patchArticle,
postComment,
postArticle,
} = require("./controllers/articles.controllers");
const { getTopics } = require("./controllers/news.controllers");
const errors = require("./error-handlers/errors");
const {
getUsers,
getUserByUsername,
} = require("./controllers/users.controllers");
const {
removeCommentById,
patchCommentVote,
} = require("./controllers/comments.controllers");
//API
app.get("/api", (req, res, next) => {
readFile("./endpoints.json").then((body) => {
const endpoints = JSON.parse(body);
res.status(200).send({ endpoints });
});
});
//TOPICS
app.get("/api/topics", getTopics);
//ARTICLES
app.get("/api/articles", getArticles);
app.get("/api/articles/:article_id", getArticlesById);
app.get("/api/articles/:article_id/comments", getCommentsByArticleId);
app.patch("/api/articles/:article_id", patchArticle);
app.post("/api/articles/:article_id/comments", postComment);
app.post("/api/articles", postArticle);
//USERS
app.get("/api/users", getUsers);
app.get("/api/users/:username", getUserByUsername);
//COMMENTS
app.delete("/api/comments/:comment_id", removeCommentById);
app.patch("/api/comments/:comment_id", patchCommentVote);
app.all("/*", (req, res) => {
res.status(404).send({ msg: "Path not found." });
});
app.use(errors.psqlErrorHandler);
app.use(errors.customErrorHandler);
app.use(errors.error500Handler);
module.exports = app;