-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
77 lines (68 loc) · 2.06 KB
/
server.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
const express = require("express");
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const exphbs = require("express-handlebars");
const path = require("path");
const app = express();
const fileUpload = require("express-fileupload");
const movieRoutes = require("./routes/movieRoutes");
const userRoutes = require("./routes/userRoutes");
const { connectDB } = require("./db/connectDB");
const session = require("express-session");
const splittingBySpace = require("./views/helpers/splittingBySpace");
const splitting = require("./views/helpers/splitting");
const convertToMin = require("./views/helpers/convertToMin");
const moment = require("./views/helpers/moment");
const iterateThroughArray = require("./views/helpers/iterateThroughArray");
const subtract = require("./views/helpers/subtract");
const multiply = require("./views/helpers/multiply");
const {userSession, determineMethod} = require("./middleware/userSession");
const { ConnectionStates } = require("mongoose");
const PORT = process.env.PORT || 8080;
app.engine(
"hbs",
exphbs({
extname: ".hbs",
defaultLayout: "main",
helpers: {
splittingBySpace: splittingBySpace,
splitting: splitting,
convertToMin: convertToMin,
iterateThroughArray: iterateThroughArray,
moment: moment,
subtract: subtract,
multiply: multiply
},
})
);
app.set("view engine", "hbs");
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "public")));
app.use(
session({
secret: `${process.env.SECRET_KEY}`,
resave: true,
saveUninitialized: true,
cookie: { maxAge: 8 * 60 * 60 * 1000 },
})
);
app.use(fileUpload());
app.use(userSession);
app.use(determineMethod);
app.get("/", (req, res, next) => {
console.log("session data", res.locals.user);
next();
});
app.use("/", movieRoutes);
app.use("/user", userRoutes);
app.use((req, res) => {
res.render("error404", {
title: "Mflix | 404",
});
});
app.listen(PORT, () => {
console.log("Server started at", PORT);
connectDB();
})