-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
54 lines (44 loc) · 1.36 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
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const cookieParser = require("cookie-parser");
const helmet = require("helmet");
const xss = require("xss-clean");
const userRouter = require("./routes/userRouter");
const questionsRouter = require("./routes/questionsRouter");
const playRouter = require("./routes/playRouter");
const viewRouter = require("./routes/viewRouter");
const AppError = require("./utils/appError");
const errHandler = require("./utils/errorHandler");
const app = express();
app.set("view engine", "pug");
app.set("views", path.join(__dirname, "public", "views"));
mongoose
.connect(process.env.MONGO_SRV, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.then(() => {
console.log("MongoDB connected successfully!");
})
.catch((err) => {
console.log(err);
});
app.use(helmet());
app.use(xss());
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.use(cookieParser());
// Mounting routers
app.use("/api/v1/users", userRouter);
app.use("/api/v1/questions", questionsRouter);
app.use("/api/v1/play", playRouter);
app.use("/", viewRouter);
app.all("*", (req, res) => {
throw new AppError("This page does not exist on the server!", 404);
});
// Error handler
app.use(errHandler);
module.exports = app;