-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
90 lines (78 loc) · 3.2 KB
/
index.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
//DEPENDENCIES
// These are modules required by other modules for them to work
const moment = require("moment");
const express = require("express");
const mongoose = require("mongoose");
const homeRoutes = require("./routes/homeRoutes");
const contactRoutes = require("./routes/contactRoutes");
const expenseTrackingRoutes = require("./routes/expenseTrackingRoutes");
const registerVehicleRoutes = require("./routes/registerVehicleRoutes");
const registerWasherRoutes = require("./routes/registerWasherRoutes");
const registerManagerRoutes = require("./routes/registerManagerRoutes");
const reportRoutes = require("./routes/reportRoutes");
const authRoutes = require("./routes/authRoutes");
const Manager = require("./models/Manager");
const passport = require("passport");
const expressSession = require("express-session")({
secret: "secret",
resave: false,
saveUninitialized: false,
});
require("dotenv").config();
//INSTANTIATIONS- Are statements to create an object
//We create a new Express app using the express function and assigning it to an app variable.
//Express is a nodejs framework for writing web apps
const app = express();
//mongoose connection
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
mongoose.connection
.on("open", () => {
console.log("Mongoose connection open");
})
.on("error", (err) => {
console.log(`Connection error: ${err.message}`);
});
//CONFIGURATIONS OR SETTINGS- Are custom application based settings
//We configure app.js to use Pug as a layout engine and to look for templates inside the views folder:
app.locals.moment = moment;
app.set("view engine", "pug");
app.set("views", "./views");
//MIDDLEWARE- These are used to determine the flow of the response cycle
app.use(express.urlencoded({ extended: true }));
// css access
app.use(express.static("public"));
app.use(expressSession);
app.use(passport.initialize());
app.use(passport.session());
passport.use(Manager.createStrategy());
passport.serializeUser(Manager.serializeUser());
passport.deserializeUser(Manager.deserializeUser());
// this custom middleware checks if the person trying to access the site has a session or not
var loginChecker = function (req, res, next) {
if (req.path != "/login" && req.path != "/contact" && !req.session.user) {
res.redirect("/login");
}
next();
};
app.use(loginChecker);
// ROUTES
//We tell the app that, whenever it receives a request from forward slash anything, it should use the routes file.
app.use("/", homeRoutes);
app.use("/contact", contactRoutes);
app.use("/expensetracking", expenseTrackingRoutes);
app.use("/vehicleregistration", registerVehicleRoutes);
app.use("/washerregistration", registerWasherRoutes);
app.use("/report", reportRoutes);
app.use("/", authRoutes);
app.use("/registermanager", registerManagerRoutes);
//The code below is use to handle non existing routes.
app.get("*", (req, res) => {
res.status(404).send("This is an invalid URL, please try another");
});
//Binds and listens for connections on the specified host and port.In my case port 2000
app.listen(2000, () => {
console.log("I am listening in on port 2000");
});