-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
100 lines (85 loc) · 3.36 KB
/
Copy pathapp.js
File metadata and controls
100 lines (85 loc) · 3.36 KB
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
const express = require("express");
const morgan = require("morgan");
const path = require("path");
const rateLimit = require("express-rate-limit");
const mongoSanitize = require("express-mongo-sanitize");
const helmet = require("helmet");
const { xss } = require("express-xss-sanitizer");
const hpp = require("hpp");
const errorHandler = require("./controllers/errorHandler");
const apiRouter = require("./routers/apiRouter");
const webhookController = require("./controllers/webhookController");
// TODO: App
/**
* TODO LIST
* - Add feedback button
* - complete testing of app + fix unit testing (removed bc not mocking Email)
* - fix errors (determine what to do when they occur, when email fails, we don't get a console error)
* - Find a way to reduce artifact zip size
*
* DONE (double check at the end of development):
* - Add security packages like helmet, rate limiter, etc...
* - Use cosmodb database instead of dev database
* - Add lectures and practicals (or other)
* - Determine how to handle failed operations when processing alerts (Rehaul logging)
* - Remove alert paused status (maybe) and create another field instead
* - Restrict alert creation only for enrollable terms (April 19, 2025 version) --> need to automatically de-activate alerts when enrollment ends
* - Create start-of-term schedules
* - Review process.env.production and refactor for test server
* - Determine how to keep course data updated efficiently when no alert is set on it --> Update coursedata after activation
* - Authentication? no auth, just use compass
* - Enable/disable creation of alerts based on enrollment timing
* - IMPORTANT: Refactor so that most operations are done in the scheduler controller (rn its too messy)
* - Determine how to run course requests in parallel
*/
const app = express();
// Serving static files
app.use(express.static(path.join(__dirname, "public")));
// Development Logging
if (process.env.NODE_ENV === "development") {
const morganOptions = {
skip: (req) => req.url.startsWith("/_next") || req.url.startsWith("/__next"),
};
app.use(morgan("dev", morganOptions));
}
// Webhooks (Apparently does not need protections)
app.post("/webhooks", express.raw({ type: "application/json" }), webhookController.handleWebhooks);
// Set security HTTP headers
// TODO: Figure out how to set headers with Google Analytics
// app.use(
// helmet({
// contentSecurityPolicy: {
// useDefaults: true,
// directives: {
// scriptSrc: [
// "'self'",
// "https://www.googletagmanager.com",
// "https://www.google-analytics.com",
// ],
// connectSrc: ["'self'", "https://www.google-analytics.com"],
// },
// },
// })
// );
// Limit requests from same person
const limiter = rateLimit({
max: 45,
windowMs: 60 * 1000, // 1 min
message: { status: 429, message: "Too many requests sent, please try again in a bit!" },
});
app.use("/api", limiter);
// Request body parsing
const bodySizeLimit = "10kb";
app.use(express.json({ limit: bodySizeLimit }));
app.use(express.urlencoded({ extended: true, limit: bodySizeLimit }));
// Data sanitization against NoSQL query injection
app.use(mongoSanitize());
// Data sanitization against XSS
app.use(xss());
// Prevent parameter pollution
app.use(hpp());
// API routes
app.use("/api/v1", apiRouter);
// Global error handler
app.use(errorHandler);
module.exports = app;