forked from Dryft-bits/ChronoFactorem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (54 loc) · 1.8 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
const express = require("express");
const connectDB = require("./server/config/db.js");
const cors = require("cors");
const passport = require("passport");
const bodyParser = require("body-parser");
const cookieSession = require("cookie-session");
const path = require("path");
const auth = require("./server/routes/api/auth.js");
const helForm = require("./server/routes/api/helForm.js");
const helData = require("./server/routes/api/helData.js");
const timetable = require("./server/routes/api/timetable.js");
const configuration = require("./server/config/constants.js");
/* Express setup */
const app = express();
app.use(express.urlencoded({ extended: false }));
const PORT = process.env.PORT || 5000;
app.use(cors({ origin: true, credentials: true }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
/* Connect to database */
const Student = require("./server/models/Student.js");
const Login = require("./server/models/Login.js");
if (process.env.NODE_ENV !== "test") {
connectDB();
}
app.use(
cookieSession({
maxAge: 60 * 60 * 1000,
keys: [configuration.cookieKey]
})
);
/* Passport stuff */
const passportJS = require("./passport.js");
app.use(passport.initialize());
app.use(passport.session({ saveUninitialized: false, resave: false }));
/* Define Routes */
app.use("/api", auth);
app.use("/api/helform", helForm);
app.use("/api/helData", helData);
app.use("/api/timetable", timetable);
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
const server = app.listen(PORT, () =>
console.log(`Server is listening on port ${PORT}`)
);
function stop() {
server.close();
}
module.exports = app;
module.exports.stop = stop;