-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (47 loc) · 1.31 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
const express = require("express");
const path = require("path");
const favicon = require("express-favicon");
const cors = require("cors");
require("dotenv").config();
const HttpError = require("./models/http-error");
// BODYPARSER
const bodyParser = require("body-parser");
// ROUTES
const affinityRoutes = require("./api/routes/affinity-route");
const nameRoutes = require("./api/routes/name-route");
const app = express();
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, "public")));
app.use(favicon(__dirname + "/assets/favicon.ico"));
app.use(cors());
// ROUTES
app.use("/api/affinity", affinityRoutes);
app.use("/api/name-meaning", nameRoutes);
app.get("/", (req, res, next) => {
res.status(200);
res.sendFile(path.join(__dirname, "views", "index.html"));
});
app.use((req, res, next) => {
const error = new HttpError(
"Could not find this route",
["Could not find this route"],
404
);
throw error;
});
app.use("/api", (error, req, res, next) => {
res.status(error.code || 500);
res.json({
status: "false",
error: {
message: error.message,
messages: error.messages,
code: error.code || 500,
},
});
});
app.use((error, req, res, next) => {
res.status(400);
res.sendFile(path.join(__dirname, "views", "404.html"));
});
module.exports = app;