-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
108 lines (91 loc) · 2.52 KB
/
app.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
const express = require("express");
const session = require("express-session");
const mongoose = require("mongoose");
require("dotenv").config();
// Local packages
const { passportConfig, passport } = require("./configs/passport-config");
const userRoutes = require("./routes/userRoutes");
const carRoutes = require("./routes/carRoutes");
const adminRoutes = require("./routes/adminRoutes");
const rentRoutes = require("./routes/rentRoutes");
const mainApp = require(__dirname + "/utility/main");
const HttpError = require("./models/http-error");
const Car = require("./models/car");
const app = express();
app.use(
session({
secret: process.env.APP_SECRET || "123xyz",
resave: false,
saveUninitialized: false,
})
);
app.set("view engine", "ejs");
app.use(express.json({ limit: "50mb" }));
app.use(
express.urlencoded({
limit: "100mb",
extended: false,
parameterLimit: 500000,
})
);
app.use(express.static("public"));
// Configure passport
passportConfig(app);
// ===========================
// ===========================
// Routes section
// ===========================
// ===========================
app.get("/", async function (req, res) {
let cars = await mainApp.getAllDocs(Car);
if (req.isAuthenticated()) {
let user = await mainApp.processUser(req.user);
let homePack = {
cars: cars,
user: user,
auth: true,
};
res.render("home", homePack);
} else {
let homePack = {
cars: cars,
auth: false,
};
res.render("home", homePack);
}
});
app.use("/", userRoutes);
app.use("/", carRoutes);
app.use("/", adminRoutes);
app.use("/", rentRoutes);
// Error for non-existing route
app.use((req, res, next) => {
const error = new HttpError("This route doesn't exist.", 404);
throw error;
});
// Middlware for error handling
app.use((err, req, res, next) => {
if (res.headerSent) {
return next(err);
}
let code = err.code || 500;
let message = err.message || "Oops, an unknown error occured.";
res.render("error", { message, code });
});
// Please apply your mongo key here
mongoose
.connect(process.env.MONGO_KEY, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log("Connected to DB successfully.");
let port = process.env.PORT;
if (port == null || port == "") {
port = 8888;
}
app.listen(port, function () {
console.log("Server is up and running");
});
})
.catch((e) => {
console.log("Failed to connect DB.");
console.log(e);
});