This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
102 lines (82 loc) · 2.98 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
91
92
93
94
95
96
97
98
99
100
101
102
/* ** NOTES **
1. Use the "npm init" command to create a package.json file
2. "npm install --save-dev nodemon" command to install nodemon (see package.json)
3. Use the "npm run devStart" command to start the server (see package.json)
nodemon can be acti
*/
export { serverdata };
import { startWebsocketserver } from "./web_socket/handlers.js";
const host = "localhost";
const port = 3000;
const webSocketPort = 3443;
import express from "express";
import expressLayouts from "express-ejs-layouts";
import mongoose from "mongoose";
import mongoDBStore from "connect-mongodb-session";
import session from "express-session";
import passport from "passport";
import flash from "connect-flash";
import status from "express-status-monitor";
//const express = require("express");
const app = express();
app.use(status());
app.use(express.static("public")); // Middleware function that serves static files (e.g. css files) https://expressjs.com/en/starter/static-files.html
app.use(express.urlencoded({ extended: true })); // Middleware function that parses the body of a request (e.g. form data)
app.use(express.json({ limit: "100mb" })); // This allows us to parse json data
//Passport config
import { checkPassport } from "./config/passport.js";
checkPassport(passport);
//MongoDB atlas config
import { MongoURI as db } from "./config/keys.js";
import { sessionsURI } from "./config/keys.js";
//Connect to MongoDB
mongoose
.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => console.log("MongoDB connected"))
.catch((err) => console.log(err));
//Storage for sessions
const sessiontStore = mongoDBStore(session);
const store = new sessiontStore({
uri: sessionsURI,
collection: "Gridserver",
});
//EJS setup
app.use(expressLayouts);
app.set("view engine", "ejs"); // Makes .ejs files possible to use
app.use(flash());
//Express session - passport session (webpage)
app.use(
session({
name: "Gridserver",
secret: "GridSecret",
resave: false,
saveUninitialized: false,
store: store,
cookie: { maxAge: 1000 * 60 * 60 * 24 * 7 }, // 1 week
})
);
//Passport middleware
app.use(passport.initialize()); //This is needed to initialize passport
app.use(passport.session()); //This is needed to keep the user logged in
import { checkRole } from "./config/authentication.js";
//Buyer page routing
import { buyerRouter } from "./routes/buyer.js";
app.use("/buyer", checkRole("buyer"), buyerRouter);
//Index page routing
import { router } from "./routes/index.js";
app.use("/", router);
//Admin page routing
import { adminRouter } from "./routes/admin.js";
app.use("/admin", checkRole("admin"), adminRouter);
const serverstart = Date.now();
//Data for server monitoring on admin page
const serverdata = {
failedjobs: 0,
jobsComputed: 0,
serverstart: serverstart,
connectedworkers: [],
};
app.listen(port, () =>
console.log(`Server has been started on http://${host}:${port}`)
);
startWebsocketserver(host, webSocketPort);