This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
78 lines (65 loc) · 3.07 KB
/
Copy pathapp.js
File metadata and controls
78 lines (65 loc) · 3.07 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
require("dotenv").config();
let express = require("express");
let app = express(); //create express app
app.set("view engine", "ejs"); //use EJS for view engine
app.use(express.static("public")) //static files served from /public/, (eg. url.com/img/example.png serves from /public/img/example.png)
let session = require('express-session');
app.use(session({secret: "Shh, its a secret!"})); //REPLACE THE SECRET WITH SOMETHING SECURE LATER
let bodyParser = require("body-parser");
app.use(bodyParser.json()); //body parser for json
app.use(bodyParser.urlencoded()); //body parser for urlencoded
const { MongoClient, ObjectID } = require("mongodb");
const { Router } = require("express");
if(process.env.PRODUCTION) {
console.log("Running on production server...");
var protocol = "mongodb";
var mongoHost = "localhost";
}
else {
console.log("Running for development...");
var protocol = "mongodb+srv";
var mongoHost = "cluster0.kfvlj.mongodb.net";
}
const uri = `${protocol}://admin:${process.env.MONGO_PASSWORD}@${mongoHost}/merry-tutor?retryWrites=true&w=majority`;
const mongoClient = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
/* Middleware to attach user data to all requests */
//get the users collection the same way as auth.js
let usersCollection;
mongoClient.connect(err => {
if (err) console.log(err);
usersCollection = mongoClient.db("merry-tutor").collection("Users");
console.log("Users Middleware Connected to Users collection");
});
//app.use takes a function that is added to the path of a request. When we call next() it goes to the next function in the path
app.use(async (req, res, next) => {
if(req.session.userId) req.user = await usersCollection.findOne(ObjectID(req.session.userId)); //if there is a user id, set req.user to that user data object
if (!(req.path.startsWith("/auth") || req.path.startsWith("/login")) && req.user && req.user.roles.length == 0) { //make sure that the user completes the auth flow, people without roles are bad and arent allowed to do anything
res.redirect("/login?firstTimeFlow");
return;
}
next();
})
//routes
app.use("/tutee", require("./routes/tutee.js")); //anything send to /student... will be sent to student.jsnpm
app.use("/summary", require("./routes/summary.js"));
app.use("/auth", require("./routes/auth.js"));
app.use("/autocomplete", require("./routes/autocomplete.js"));
app.use("/board", require("./routes/board.js"));
app.use("/parent", require("./routes/parent.js"));
app.use("/export", require("./routes/export.js"));
app.use("/profile", require("./routes/profile.js"));
app.get("/", (req,res) => {
res.render("index.ejs", {user: req.user});
})
app.get("/login", (req,res) => {
res.render("login");
})
//error handler
app.use((err, req, res, next) => {
console.log(err.stack);
res.status(500).render("error", {code: 500, description: "Internal Server Error"})
})
app.use((req,res,next) => {
res.status(404).render("error", {code: 404, description: "The Requested Page Was Not Found"})
})
app.listen(8080, () => {console.log("Listening on port 8080")});