forked from hackforla/VRMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
127 lines (103 loc) · 3.86 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// server.js - Entry point for our application
// Load in all of our node modules. Their uses are explained below as they are called.
const express = require("express");
require("dotenv").config();
const mongoose = require("mongoose");
const morgan = require("morgan");
const bodyParser = require("body-parser");
// const helmet = require('helmet');
// const cors = require('cors');
const path = require("path");
const cron = require("node-cron");
const fetch = require("node-fetch");
// Create a new application using the Express framework
const app = express();
// Load config variables
const { DATABASE_URL, PORT } = require("./config/database");
// Required to view Request Body (req.body) in JSON
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// HTTP Request Logger
app.use(morgan("dev"));
// Hide sensitive Header data
// app.use(helmet());
// Cross-Origin-Resource-Sharing
// app.use(cors());
// Let mongoose access Promises
mongoose.Promise = global.Promise;
// WORKERS
const runOpenCheckinWorker = require("./workers/openCheckins")(cron, fetch);
const runCloseCheckinWorker = require("./workers/closeCheckins")(cron, fetch);
const runCreateRecurringEventsWorker = require("./workers/createRecurringEvents")(cron, fetch);
const runSlackBot = require("./workers/slackbot")(fetch);
// ROUTES
const eventsRouter = require("./routers/events.router");
const checkInsRouter = require("./routers/checkIns.router");
const answersRouter = require("./routers/answers.router");
const usersRouter = require("./routers/users.router");
const questionsRouter = require("./routers/questions.router");
const checkUserRouter = require("./routers/checkUser.router");
const grantPermissionRouter = require("./routers/grantpermission.router");
const projectsRouter = require("./routers/projects.router");
const recurringEventsRouter = require("./routers/recurringEvents.router");
const projectTeamMembersRouter = require("./routers/projectTeamMembers.router");
app.use("/api/events", eventsRouter);
app.use("/api/checkins", checkInsRouter);
app.use("/api/answers", answersRouter);
app.use("/api/users", usersRouter);
app.use("/api/questions", questionsRouter);
app.use("/api/checkuser", checkUserRouter);
app.use("/api/grantpermission", grantPermissionRouter);
app.use("/api/projects", projectsRouter);
app.use("/api/recurringevents", recurringEventsRouter);
app.use("/api/projectteammembers", projectTeamMembersRouter);
const CLIENT_BUILD_PATH = path.join(__dirname, "./client/build");
// Serve static files from the React frontend app
app.use(express.static(path.join(CLIENT_BUILD_PATH)));
// Anything that doesn't match the above, send back index.html
app.get("*", (req, res) => {
const index = path.join(CLIENT_BUILD_PATH, "index.html");
res.sendFile(index);
});
// Make the server functions available to testing
let server;
async function runServer(databaseUrl, port = PORT) {
await mongoose
.connect(databaseUrl, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
})
.catch((err) => err);
server = app
.listen(port, () => {
console.log(
`Mongoose connected from runServer() and is listening on ${port}`
);
})
.on("error", (err) => {
mongoose.disconnect();
return err;
});
}
async function closeServer() {
await mongoose.disconnect().then(() => {
return new Promise((resolve, reject) => {
console.log("Closing Mongoose connection. Bye");
server.close((err) => {
if (err) {
return reject(err);
}
resolve();
});
});
});
}
if (require.main === module) {
runServer(DATABASE_URL).catch((err) => console.error(err));
}
// app.listen(process.env.PORT || PORT, () => {
// console.log(`Server is listening on port: ${process.env.PORT || PORT}`);
// });
module.exports = { app, runServer, closeServer };