-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdormable.js
141 lines (118 loc) · 4.76 KB
/
dormable.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ==============================================================
IMPORTS AND CONFIGURATION
============================================================== */
const express = require("express");
const passport = require("passport");
const cookieParser = require("cookie-parser");
require("./modules/passportConfig")(passport);
const session = require("./modules/userSession");
const app = express();
const PORT = process.env.PORT || 3000;
const path = require("path");
//Mongoose connection to DormableDB
const mongoDB = require("./modules/mongooseConnect");
//Session configuration
app.use(cookieParser());
app.use(session);
app.use(passport.initialize());
app.use(passport.session());
// Use middleware to parse request bodies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
//Redirect http to https
if (app.get("env") === "production" || process.env.NODE_ENV === "production") {
app.set("trust proxy", 1); // trust first proxy, crucial
}
app.use(function (req, res, next) {
if (
req.get("x-forwarded-proto") !== "https" &&
(app.get("env") === "production" ||
process.env.NODE_ENV === "production")
) {
res.redirect(`https://${req.hostname}${req.url}`);
} else {
next();
}
});
//Routers for each collection
const listingDB_Router = require("./routers/listings_Router");
const listingOwnerDB_Router = require("./routers/listingOwners_Router");
const listingAdminDB_Router = require("./routers/listingAdmins_Router");
const userDB_Router = require("./routers/userInfos_Router");
const userLoginInfoDB_Router = require("./routers/userLoginInfo_Router");
const ownerResponseDB_Router = require("./routers/ownerResponse_Router");
const reviewDB_Router = require("./routers/listingReviews_Router");
//Middleware for static assets
//app.use("/vendor", express.static(path.join(__dirname, "vendor")));
//app.use("/assets", express.static(path.join(__dirname, "assets")));
app.use(express.static(path.join(__dirname, "dist")));
/* ==============================================================
FETCH/WRITE REQUESTS TO DATABASE FOR EACH COLLECTION
============================================================== */
app.use("/api/listingDB", listingDB_Router.router);
app.use("/api/listingOwnerDB", listingOwnerDB_Router);
app.use("/api/listingAdminDB", listingAdminDB_Router);
app.use("/api/userDB", userDB_Router.router);
app.use("/api/loginForm", userLoginInfoDB_Router);
app.use("/api/reviewDB", reviewDB_Router);
app.use("/api/ownerResponseDB", ownerResponseDB_Router);
/* ==============================================================
ROUTES TO INDIVIDUAL PAGES
============================================================== */
app.get("/index", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "index.html"));
});
app.get("/404", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "404.html"));
});
app.get("/explore-listing", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "explore-listing.html"));
});
app.get("/listing", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "listing.html"));
});
app.get("/login", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "login.html"));
});
app.get("/profile", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "profile.html"));
});
app.get("/request-listing", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "request-listing.html"));
});
app.get("/search-result", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "search-result.html"));
});
app.get("/about-us", (req, res) => {
res.sendFile(path.join(__dirname, "dist", "about-us.html"));
});
/* ==============================================================
MISC CONFIGURATION
============================================================== */
//Make index.html the default page
app.get("/", (req, res) => {
res.redirect("/index");
});
app.get("/index.html", (req, res) => {
res.redirect("/index");
});
//Make 404 request refer to custom 404 page
app.use(function (req, res, next) {
res.status(404).sendFile(path.join(__dirname, "./dist/404.html"));
});
//remove .html from the url
//app.use(express.static(__dirname + "/web_pages", { extensions: ["html"] }));
console.log(`Current directory: ${process.cwd()}`);
console.log(`__dirname: ${__dirname}`);
/* ==============================================================
START SERVER
==============================================================*/
//Listening to the server
app.listen(PORT, async () => {
await mongoDB(3);
console.log(
`Server is running on port ${PORT} at http://localhost:${PORT}`
);
setInterval(listingDB_Router.updateAllListingScores, 1000 * 60 * 30);
setInterval(userDB_Router.periodicUserInfoUpdate, 1000 * 60 * 60 * 24);
});