-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (79 loc) · 2.66 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
//? Third party modules
const express = require("express");
const sessions = require("express-session");
const flash = require("connect-flash");
const helmet = require("helmet");
const compression = require("compression");
const morgan = require("morgan");
const { config } = require("dotenv");
//? Core modules
const path = require("path");
const EventEmitter = require("events");
const fs = require("fs");
//? Local imports
const { connectMongoose, sessionStoreCreator } = require("./util/database");
const errorRoute = require("./routes/error");
const homeRoute = require("./routes/home");
const { adminRoutes } = require("./routes/admin");
const shopRoutes = require("./routes/shop");
const authRoutes = require("./routes/auth");
const authenticationChecker = require("./Controllers/auth/authenticationChecker");
const commonViewOptionsProviderMiddleware = require("./Controllers/middleware/commonViewOptionsProviderMiddleware");
const expressErrorController = require("./Controllers/error/expressErrorController");
const { urlencoded } = require("body-parser");
const app = express();
config();
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
//* => Request will fall down from here.
const accessLogsStream = fs.createWriteStream(
path.join(__dirname, "access.log")
);
app.use(helmet());
app.use(compression());
app.use(morgan("combined", { stream: accessLogsStream }));
app.use(urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use("/uploads", express.static(path.join(__dirname, "uploads")));
app.use(
sessions({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false,
store: sessionStoreCreator(sessions),
cookie: {
expiry: new Date().setDate(new Date().getDate + 1),
},
})
);
app.use(flash());
app.use(authenticationChecker);
app.use(commonViewOptionsProviderMiddleware);
app.use("/admin", adminRoutes);
app.use("/shop", shopRoutes);
app.use("/auth", authRoutes);
app.use(homeRoute);
app.use(errorRoute);
//* THIS MUST BE KEPT LAST IN THE MIDDLEWARE STACK.
app.use(expressErrorController);
//? "events" core module check
const serverStarted = new EventEmitter();
serverStarted.on("server_on", () => {
console.log(`Express Server is running on port ${process.env.PORT}`);
});
const databaseConnectionSuccessCallback = async () => {
app.listen(process.env.PORT, (err) => {
if (err) {
console.log(err);
} else {
serverStarted.emit("server_on");
}
});
};
const databaseConnectionFailureCallback = (error) => {
console.log("The error is: ", error);
};
connectMongoose(
databaseConnectionSuccessCallback,
databaseConnectionFailureCallback
);