-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (90 loc) · 2.74 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
91
92
93
94
95
96
97
98
99
100
const express = require("express");
const path = require("path");
const csrf = require("csurf");
const flash = require("connect-flash");
const mongoose = require("mongoose");
const helmet = require("helmet");
const compression = require("compression");
const exphbs = require("express-handlebars");
const session = require("express-session");
const MongoStore = require("connect-mongodb-session")(session);
const homeRoutes = require("./routes/home.routes");
const cardRoutes = require("./routes/card.routes");
const addRoutes = require("./routes/add.routes");
const ordersRoutes = require("./routes/orders.routes");
const coursesRoutes = require("./routes/courses.routes");
const authRoutes = require("./routes/auth.routes");
const profileRoutes = require("./routes/profile.routes");
const errorMiddleware = require("./middleware/error.middleware");
const varMiddleware = require("./middleware/variables.midleware");
const fileMiddleware = require("./middleware/file.middleware");
const UserMiddleware = require("./middleware/user.midleware");
const keys = require("./keys");
const app = express();
const store = new MongoStore({
collection: "sessions",
uri: keys.MONGODB_URL,
});
const hbs = exphbs.create({
defaultLayout: "main",
extname: "hbs",
helpers: require("./helpers/hbs.helpers"),
});
app.engine("hbs", hbs.engine);
app.set("view engine", "hbs");
app.set("views", "views");
app.use(express.static(path.join(__dirname, "public")));
app.use("/images", express.static(path.join(__dirname, "images")));
app.use(express.urlencoded({ extended: true }));
app.use(
session({
secret: keys.SESSION_SECRET,
resave: false,
saveUninitialized: false,
store,
})
);
app.use(fileMiddleware.single("avatar"));
app.use(csrf());
app.use(flash());
app.use(
helmet({
contentSecurityPolicy: {
directives: {
...helmet.contentSecurityPolicy.getDefaultDirectives(),
"img-src": ["'self'", "https:"],
"script-src-elem": [
"'self'",
"https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js",
"'unsafe-inline'",
],
},
},
})
);
app.use(compression());
app.use(varMiddleware);
app.use(UserMiddleware);
app.use("/", homeRoutes);
app.use("/add", addRoutes);
app.use("/courses", coursesRoutes);
app.use("/card", cardRoutes);
app.use("/orders", ordersRoutes);
app.use("/auth", authRoutes);
app.use("/profile", profileRoutes);
app.use(errorMiddleware);
const PORT = process.env.PORT || 3000;
async function start() {
try {
await mongoose.connect(keys.MONGODB_URL, {
useNewUrlParser: true,
useFindAndModify: false,
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
} catch (e) {
console.log(e);
}
}
start();