-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
90 lines (80 loc) · 2.53 KB
/
server.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//!ADD VALIDATION
// * NPM Packages
const express = require("express");
const mongoose = require("mongoose");
require("dotenv").config();
const passport = require("passport");
const cookieSession = require("cookie-session");
const cookieParser = require("cookie-parser");
const cors = require("cors");
const connectFlash = require("connect-flash");
const path = require("path");
const app = express();
// * Routes import
const auth = require("./routes/auth");
const participants = require("./routes/participants");
const quiz = require("./routes/quiz");
const question = require("./routes/questions");
const questionBank = require("./routes/questionBank");
// * Middleware
app.use(cookieParser());
app.use(cors({ origin: `${process.env.CLIENT_URL}`, credentials: true }));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", `${process.env.CLIENT_URL}`);
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
next();
});
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(
cookieSession({
maxAge: 1000 * 60 * 60 * 24, //24 HOURS
secret: process.env.COOKIE_SECRET,
})
);
app.use(connectFlash());
// * Passport Setup
require("./config/passportParticipants");
app.use(passport.initialize());
app.use(passport.session());
// * Start Server
const PORT = process.env.PORT || 5000;
const server = app.listen(PORT, console.log(`Server started on ${PORT}`));
mongoose.connect(
process.env.MONGO_URI,
// "mongodb://localhost:27017/portal",
{
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
},
(err) => {
if (err) return console.log("Connection to DB failed \n", err);
return console.log("Connected to DB.");
}
);
// * Routes setup
app.use("/api/auth", auth);
app.use("/api/participants", participants);
app.use("/api/quiz", quiz);
app.use("/api/question", question);
app.use("/api/questionBank", questionBank);
app.use(express.static(path.resolve(__dirname, "Client", "build")));
app.get("/*", (req, res) => {
// res.sendFile("index.html");
res.sendFile(path.resolve(__dirname, "Client", "build", "index.html"));
});
process
.on("unhandledRejection", (reason, p) => {
console.error(reason, "Unhandled Rejection at Promise", p);
})
.on("uncaughtException", (err) => {
console.error(err, "Uncaught Exception thrown");
process.exit(1);
});