-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
75 lines (61 loc) · 1.64 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
require("dotenv").config();
const express = require("express");
const cookieParser = require("cookie-parser");
const methodOverride = require("method-override");
const session = require("express-session");
const flash = require("express-flash");
const moment = require("moment");
const expressLayouts = require("express-ejs-layouts");
const app = express();
const PORT = process.env.PORT || 3000;
app.locals.moment = moment;
const { adminRouter, userRouter, staticRouter } = require("./routes");
const connectDB = require("./config/db");
const { get404 } = require("./controllers");
const {
isUserAuthenticated,
isNotUserBlocked,
isNotUserAuthenticated,
isAdminAuthenticated,
setAdminAvatar,
setUserSeo,
setAdminSeo,
} = require("./middlewares");
connectDB();
app.set("view engine", "ejs");
app.use(expressLayouts);
app.use(
session({
secret: "secret",
resave: false,
saveUninitialized: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 7,
},
})
);
app.use(flash());
app.use(cookieParser());
app.use(express.json());
app.use(methodOverride("_method"));
app.use(express.urlencoded({ extended: false }));
app.use(express.static("public"));
// app.use(express.static("public/assets/css"));
app.use(
"/admin",
isAdminAuthenticated,
setAdminSeo,
setAdminAvatar,
adminRouter
);
app.use("/user", isUserAuthenticated, isNotUserBlocked, setUserSeo, userRouter);
app.use("/", setUserSeo, staticRouter);
app.get("*", get404);
app.listen(PORT, (err) => {
if (err)
console.log(
"Something went wrong. Couldn't start the server\nError: ",
err
);
else console.log("Server running on port: " + PORT);
});