-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (62 loc) · 2.14 KB
/
index.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
69
70
71
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
//Set Port to 3500
const PORT = process.env.PORT || 3500;
//Cors
const cors = require("cors");
const corsOptions = require("./config/corsOptions");
//Middleware
const { logger } = require("./middleware/logEvents");
const errorHandler = require("./middleware/errorHandler");
const verifyJWT = require("./middleware/verifyJWT");
const cookieParser = require("cookie-parser");
const credentials = require("./middleware/credentials");
const mongoose = require("mongoose");
const connectDB = require("./config/dbConn");
const allowedOrigins = require("./config/allowedOrigins");
// Connect to database
connectDB();
app.use(logger);
app.use(credentials);
app.use(cors(corsOptions));
// COOKIE MIDDLEWARE:
app.use(cookieParser());
// BUILT-IN FORMDATA:
app.use(express.urlencoded({ extended: false }));
// BUILT-IN JSON DATA:
app.use(express.json());
// SERVE STATIC FILES
app.use("/", express.static(path.join(__dirname, "/public")));
// Routes:
app.use("/", require("./routes/root"));
app.use("/register", require("./routes/api/register"));
app.use("/auth", require("./routes/api/auth"));
app.use("/logout", require("./routes/api/logout"));
app.use("/refresh", require("./routes/api/refresh"));
app.use(verifyJWT); // Everything below here requires user to be verified
app.use("/pref", require("./routes/api/pref"));
app.use("/vote", require("./routes/api/vote"));
app.use("/scrape", require("./routes/api/scrape"));
app.use("/users", require("./routes/api/users"));
// app.use("/lists", require("./routes/api/lists"));
// app.use("/scrape", require("./routes/api/scrape"));
// 404
app.all("*", (req, res) => {
res.status(404);
if (req.accepts("html")) {
res.sendFile(path.join(__dirname, "views", "404.html"));
} else if (req.accepts("json")) {
res.json({ error: "404 not found" });
} else {
res.type("txt").send("404 Not Found");
}
});
// Error handler
app.use(errorHandler);
// Open connection to DB
mongoose.connection.once("open", () => {
console.log("Connected to database.");
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
});